2010-07-16 41 views
2

我有一个订单作为XDocument,我只是想把它放在消息的正文中并发送给MSMQ队列。我已经有效地序列化了订单对象,现在我只想发送它。这可能吗?如何通过MSMQ发送XDocument(使用WCF)?

我在这里使用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); 
} 

回答

1

我在Windows 7盒子上开发同样的问题。它将我的XML字符串放入另一个xml中。一切正常,在服务器2003年。

我终于能够解决这个问题。似乎有两种方法可以做到这一点。两者都涉及将Formatter设置为XmlMessageFormatter。您可以在MessageQueue上设置Formatter,也可以在发送之前以及在您查看/接收之后将其设置为消息。

messageQueue.Formatter = new System.Messaging.XmlMessageFormatter(new Type[] { typeof(System.String) }); 
2

XDocument是一个方便的XML数据封装。没有必要序列化XDocument,只需将XML数据作为字符串发送,使用XDocument.ToString()

+0

我试过之前,我在这里发布的问题,但它包装整个事情在标记。我需要与XDoc中完全相同的xml ... – autonomatt 2010-07-19 08:13:38