【问题标题】:Microsoft MessageQueue Formatter property is always nullMicrosoft MessageQueue Formatter 属性始终为空
【发布时间】:2023-03-25 23:27:01
【问题描述】:

我在基本的 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,这是我最需要的。

【问题讨论】:

  • 但是...消息是使用 MessageQueue.Formatter 属性“反序列化”的,不是吗?如果有,是否在接收代码中初始化了MessageQueue.Formatter属性(queue.Formatter)?
  • @JuanMellado:不,我没有。实际上我认为这是自动完成的,因为在文档中它说它默认为 XmlMessageFormatter
  • 是的,但你仍然可以这样做((XmlMessageFormatter)queue.Formatter).TargetTypes = new Type[1] { typeof(string) }; [未测试]
  • 是的,这行得通!谢谢!如果您将其发布为答案,我可以奖励您赏金。

标签: c# .net ipc message-queue


【解决方案1】:

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

((XmlMessageFormatter)queue.Formatter).TargetTypes = new Type[1] { typeof(string) };

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-04
    • 2021-07-04
    • 2021-10-07
    相关资源
    最近更新 更多