1

我对基本的 Microsoft 消息队列实现有以下问题:

在阅读端Formatter,无论我在发送端输入什么,消息的属性始终为空。

发送代码:

System.Messaging.Message m = new System.Messaging.Message("string to send");
m.Formatter = new XmlMessageFormatter( new Type[1] { typeof(string) } );
queue.Send(m, "label");

接收代码:

MessageEnumerator enumerator = queue.GetMessageEnumerator2();
while (enumerator.MoveNext())
{
    Message m = enumerator.RemoveCurrent();
    Console.WriteLine("MSQ: " + m.Label);

    Console.WriteLine("Formatter: " + m.Formatter.GetType().ToString()); // crash because formatter property is null

    Console.WriteLine("Body: " + m.Body); //also crashes since formatter is null
}

由于Formatter为空,我也无法得到m.Body我最需要的。

4

1 回答 1

4

使用该属性对消息进行反序列化。MessageQueue.Formatter所以必须在接收代码中初始化MessageQueue.Formatter属性(queue.Formatter ):

((XmlMessageFormatter)queue.Formatter).TargetTypes = new Type[1] { typeof(string) };
于 2013-01-02T10:57:38.940 回答