代码之家  ›  专栏  ›  技术社区  ›  autonomatt

如何通过msmq(使用wcf)发送xdocument?

  •  2
  • autonomatt  · 技术社区  · 16 年前

    我有一个作为xdocument的订单,我只想将它粘贴到消息体中,并将其发送到msmq队列。我已经有效地序列化了order对象,现在我只想发送它。这有可能吗?

    我在这里使用的是WCF,但是我很乐意使用一个简单的老的msmq解决方案。我在这里收到一个错误,指示无法序列化xdocument…显然不能这样做,但是我如何将xdocument放到消息体中呢?我需要滚动自己的序列化程序吗?

    public void SendOrder(XDocument order)
    {
        var address = new EndpointAddress(@"msmq.formatname:DIRECT=OS:myServer\private$\myQueue");
    
        var binding = new MsmqIntegrationBinding();
        binding.Security.Mode = MsmqIntegrationSecurityMode.None; 
        binding.ExactlyOnce = false;
        binding.Durable = false;
    
        var channelFactory = new ChannelFactory<IOrderSubmitter>(binding, address);
        var channel = channelFactory.CreateChannel();
    
        var message = new MsmqMessage<XDocument>(order);
        message.Label = "Really Big Order with lots of profit";
        message.BodyType = (int)System.Runtime.InteropServices.VarEnum.VT_ARRAY;
    
        using (var scope = new TransactionScope(TransactionScopeOption.Required))
        {
            channel.SubmitOrder(message);
            scope.Complete();
        }
    }
    
    [ServiceContractAttribute(Namespace = "http://my.namespace.com", Name = "Hello")]
    public interface IOrderSubmitter
    {
        [OperationContract(IsOneWay = true)]
        void SubmitOrder(MsmqMessage<XDocument> message);
    }
    
    2 回复  |  直到 16 年前
        1
  •  1
  •   Adrian    16 年前

    我在Windows7盒子上开发时遇到了同样的问题。它将我的XML字符串放入另一个XML中。在Server2003中一切正常。

    我终于能解决这个问题了。似乎有两种方法可以做到这一点。两者都涉及将格式化程序设置为xmlmessageformatter。您可以在消息队列上设置格式化程序,也可以在发送前和查看/接收后在消息上设置格式化程序。

    messageQueue.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(System.String) });
    
        2
  •  2
  •   Panagiotis Kanavos    16 年前

    xdocument是XML数据的方便包装器。不需要序列化XDocument,只需将XML数据作为字符串发送,使用 XDocument.ToString()