WCF不处理二进制格式的MSMQ消息 [英] WCF not processing MSMQ message with binary format

查看:51
本文介绍了WCF不处理二进制格式的MSMQ消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个检索MSMQ消息的WCF Windows服务。似乎没有调用SubmitPurchaseOrderInMessage,也没有在队列中看到任何消息。代码如下所示。



WCF类:



I have a WCF Windows Service that retrieve MSMQ messages. The SubmitPurchaseOrderInMessage doesn't seem to get called neither do I see any messages in the queue. Code is shown below.

WCF class:

public class OrderProcessorService : IOrderProcessor
   {


       [OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)]
       [ServiceKnownType(typeof(MyOrder))]
       public void SubmitPurchaseOrderInMessage(MsmqMessage<MyOrder> ordermsg)
       {
           MyOrder po = (MyOrder)ordermsg.Body;
           Console.WriteLine("Processing id:{0}, name:{1} ", po.ID, po.Name);
       }





       public static void Main()
       {
           //init queue
           if (!MessageQueue.Exists(Constants.QUEUE_PATH)) MessageQueue.Create(Constants.QUEUE_PATH, true);


           //init wcf host via code
           Uri baseUri = new Uri("http://localhost:7878/msmqsvc");
           using (ServiceHost host = new ServiceHost(typeof(OrderProcessorService),baseUri))
           {
               //add metadata behavior
               ServiceMetadataBehavior smb = new ServiceMetadataBehavior(){ HttpGetEnabled=true};
               host.Description.Behaviors.Add(smb);

               //add service endpoint
               MsmqIntegrationBinding binding = new MsmqIntegrationBinding(MsmqIntegrationSecurityMode.None);
               binding.SerializationFormat = MsmqMessageSerializationFormat.Binary;
               host.AddServiceEndpoint(typeof(ClassLib.IOrderProcessor), binding, "msmq.formatname:DIRECT=OS:" + Constants.QUEUE_PATH);

               host.Open();


               // The service can now be accessed.
               Console.WriteLine("The service is ready.");
               Console.WriteLine("Press <ENTER> to terminate service.");
               Console.ReadLine();
               host.Close();
           }
       }

   }





接口合约:< br $>




Interface Contract:

[ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]
   [ServiceKnownType(typeof(MyOrder))]
   public interface IOrderProcessor
   {
       [OperationContract(IsOneWay = true, Action = "*")]
       void SubmitPurchaseOrderInMessage(MsmqMessage<MyOrder> msg);

   }





数组参数可以是任何可以传递的动态可序列化类型客户。我认为问题在于这个参数。如果我删除了这个参数和serializable属性以及客户端中的binding.SerializationFormat,那么eveything工作正常。



可序列化类:





The array Parameters can be any dynamic serializable type that can be passed by client. I think issue is with this parameter. If I remove this parameter and the serializable attribute and also the binding.SerializationFormat in the client, then eveything works fine.

Serializable class:

[DataContract(Namespace = "http://Microsoft.ServiceModel.Samples")]
   [Serializable]
   public class MyOrder
   {
       [DataMember]
       public string ID;

       [DataMember]
       public string Name;

       [DataMember]
       public object[] Parameters;
   }





客户:





Client :

class Program
     {
        static void Main(string[] args)
        {
            try
            {
                Run();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

        static void Run()
        {
            MsmqIntegrationBinding binding = new MsmqIntegrationBinding();
            binding.Security.Mode = MsmqIntegrationSecurityMode.None;
            binding.Security.Transport.MsmqAuthenticationMode = MsmqAuthenticationMode.None;
            binding.Security.Transport.MsmqProtectionLevel = System.Net.Security.ProtectionLevel.None;
            binding.SerializationFormat = MsmqMessageSerializationFormat.Binary;
            EndpointAddress address = new EndpointAddress("msmq.formatname:DIRECT=OS:" + Constants.QUEUE_PATH);
          
          
            ChannelFactory<ClassLib.IOrderProcessor> channelFactory = new ChannelFactory<ClassLib.IOrderProcessor>(binding, address);

            try
            {
                ClassLib.IOrderProcessor channel = channelFactory.CreateChannel();

                MyOrder order = new MyOrder();
                order.ID = DateTime.Now.Ticks.ToString();
                order.Name = "Order_" + order.ID;
               order.Parameters = new object[] { new Transaction { Amount = 108 }, new Transaction { Amount = 100 } };
                MsmqMessage<MyOrder> ordermsg = new MsmqMessage<MyOrder>(order);
                using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required))
                {
                    channel.SubmitPurchaseOrderInMessage(ordermsg);
                    scope.Complete();
                }
                Console.WriteLine("Order has been submitted:{0}", ordermsg);
            }
            catch(Exception ex)
            {

            }
            finally
            {
                channelFactory.Close();
            }

        }
    }
    [Serializable]
    public class Transaction
    {
        public int Amount { get; set; }
    }

推荐答案

这篇关于WCF不处理二进制格式的MSMQ消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆