【问题标题】:How do I resolve a InvalidOperationException when reading MSMQ using ActiveXMessageFormatter?使用 ActiveXMessageFormatter 读取 MSMQ 时如何解决 InvalidOperationException?
【发布时间】:2014-10-02 21:15:06
【问题描述】:

您将如何避免下面的 MSMQ 发送方和接收方创建的 InvalidOperationException?您能提供的任何帮助将不胜感激!

我感觉它可能与发送者的 BodyType 属性有关,但我不知道该属性的正确值域。

发件人代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Messaging;

namespace MSMQTester
{
    class Program
    {
        static void Main(string[] args)
        {
            MessageQueue q = new MessageQueue(@"lab\test");
            q.Formatter = new ActiveXMessageFormatter();
            Message msg = new Message();
            msg.Priority = MessagePriority.High;
            msg.Body = "<hello_world />";
            msg.Label = "test1";
            q.Send(msg);
        }
    }
}

接收方代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Messaging;

namespace MSMQViewer
{
    class Program
    {
        static void Main(string[] args)
        {
            MessageQueue q = new MessageQueue(@"lab\test");
            q.Formatter = new ActiveXMessageFormatter();
            while (true)
            {
                Message msg = q.Receive();
                Console.WriteLine(msg.Body);
            }
        }
    }
}

错误信息:

C:\dev\MSMQTester\MSMQViewer\bin\Debug>MSMQViewer.exe

未处理的异常:System.InvalidOperationException:无法反序列化我 ssage 作为参数传递。无法识别序列化格式。 在 System.Messaging.ActiveXMessageFormatter.Read(消息消息) 在 System.Messaging.Message.get_Body() 在 c:\dev\MSMQTester\MSMQViewer\Pro 中的 MSMQViewer.Program.Main(String[] args) gram.cs:第 18 行

【问题讨论】:

    标签: .net msmq


    【解决方案1】:

    如果您也遇到此问题,您可以考虑使用以下解决方法:

    System.Messaging.MessageQueue queue = new System.Messaging.MessageQueue(strQueue);
    queue.Formatter = new System.Messaging.ActiveXMessageFormatter();
    queue.DefaultPropertiesToSend.Priority = System.Messaging.MessagePriority.High;
    queue.Send("  Message as string... Not a message object! :(  ");
    

    在上面的示例中,我能够在将优先级设置为高的同时传递消息,同时避免异常。

    【讨论】:

      猜你喜欢
      • 2021-03-18
      • 2013-10-25
      • 1970-01-01
      • 2012-02-18
      • 2013-08-16
      • 1970-01-01
      • 1970-01-01
      • 2017-02-16
      • 2019-12-09
      相关资源
      最近更新 更多