2010-07-16 39 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