2011-12-07 53 views
0

我已經使用MSMQ和WCF以及NetMsmqBinding來生成消息並在消息到達時將其出列。我還通過使用對象圖作爲消息主體來標準化我的解決方案。該對象包含元數據和內部有效載荷。反序列化MSMQ中使用WCF和NetMsmqBinding入隊的對象圖

我想構建一個管理工具,可以監視隊列並查看消息的內容。到目前爲止,我一直未能弄清楚如何使用System.Messaging庫將Message.Body反序列化到對象圖中。

任何想法?

+0

你到目前爲止嘗試過什麼?任何錯誤消息?另外,你看過Queue Explorer嗎? http://www.cogin.com/mq/ –

+0

默認情況下,只需訪問message.Body作爲Object,就會得到典型的InvalidOperationException「無法找到能夠閱讀此消息的格式化程序」。使用XmlMessageFormatter,它會因爲內容不是有效的XML而炸燬。使用BinaryFormatter,它也失敗,「無法反序列化作爲參數傳遞的消息。無法識別序列化格式。」我剛開始嘗試使用一種新方法,但仍然沒有運氣。 – jtalarico

+0

你可能無法做到。 NetMsmqBinding不會簡單地將對象圖序列化爲消息並排入消息。消息體的前面有一些協議位。 – JohnC

回答

1

您是否有任何範圍更改WCF服務綁定?

如果使用MsmqIntegrationBinding而不是netMsmqBinding,則可以在綁定中指定一系列格式化程序選項。例如

<service name="MyQueueListenner"> 

    <!-- Active X endpoint --> 
    <endpoint address="msmq.formatname:DIRECT=OS:.\private$\myQueue" 
       binding="msmqIntegrationBinding" 
       bindingConfiguration="ActiveXBinding" 
       contract="MyContract" /> 

    <!-- .Net endpoint--> 
    <endpoint address="msmq.formatname:DIRECT=OS:.\private$\myOtherQueue" 
       binding="msmqIntegrationBinding" 
       bindingConfiguration="DotNetBinding" 
       contract="MyContract" /> 

    </service> 
    ... 

    <msmqIntegrationBinding> 
    <binding serializationFormat="ActiveX" name="ActiveXBinding" durable="false" exactlyOnce="false"> 
     <security mode="None" /> 
    </binding> 
    <binding serializationFormat="Xml" name="DotNetBinding" durable="false" exactlyOnce="false"> 
     <security mode="None" /> 
    </binding> 
    </msmqIntegrationBinding> 

這可以讓你全方位格式化提供與基於System.Messaging嗅探器的最大範圍的互操作性的。

值的完整列表在這裏: http://msdn.microsoft.com/en-us/library/system.servicemodel.msmqintegration.msmqmessageserializationformat.aspx

+0

我將這個標記爲正確的答案有兩個原因。 1)這是唯一一個已經提供並且2)它讓我走上正軌。我懷疑可能有些東西可以在仍然使用NetMsmqBinding的情況下讀取和反序列化對象,但是它需要反序列化消息中的底層SOAP信封並解決命名空間衝突,合同參數等。我嘗試過這樣做,但它是很凌亂。使用MsmqIntegrationBinding在嗅探器方面要乾淨得多,但在我的消息傳遞基礎結構中,我必須對所有合同方法使用MsmqMessage 。 – jtalarico

+0

很高興我能幫上忙 –

1

我知道這是舊的,但序列化和背部的任何對象,你可以做到以下幾點:

//樣品

enter code here 

public person class 
{ 
public int Id {get;set;} 
public string Name {get;set;} 

public static person Desserialize(byte[] data) 
     { 
      person result = new person(); 
      using (MemoryStream m = new MemoryStream(data)) 
      { 
       using (BinaryReader reader = new BinaryReader(m)) 
       { 
        result.id = reader.ReadInt32(); 
        result.Name = reader.ReadString(); 

       } 
      } 
      return result; 
     } 



public byte[] Serialize() 
     { 
      using (MemoryStream m = new MemoryStream()) 
      { 
       using (BinaryWriter writer = new BinaryWriter(m)) 
       { 
        writer.Write(id); 
        writer.Write(Name); 

       } 
       return m.ToArray(); 
      } 
     } 

//你可以做 Byte [] w_byte = Person.serialize();

Person _Person = Person.Desiserile(w_byte); }