Message Queue Error: cannot find a formatter capable of reading message

前端 未结 9 1769
耶瑟儿~
耶瑟儿~ 2020-12-15 16:24

I\'m writing messages to a Message Queue in C# as follows:

queue.Send(new Message(\"message\"));

I\'m trying to read the messages as follow

相关标签:
9条回答
  • 2020-12-15 17:19

    Adding formatter solved my issue:

     public void ReceiveAsync<T>(MqReceived<T> mqReceived)
        {
            try
            {
                receiveEventHandler = (source, args) =>
                {
                    var queue = (MessageQueue)source;
                    using (Message msg = queue.EndPeek(args.AsyncResult))
                    {
                        XmlMessageFormatter formatter = new XmlMessageFormatter(new Type[] { typeof(T) });
                        msg.Formatter = formatter;
                        queue.ReceiveById(msg.Id);
                        T tMsg = (T)msg.Body;
                        mqReceived(tMsg);
    
                    }
                    queue.BeginPeek();
                };
    
                messageQueu.PeekCompleted += receiveEventHandler;
                messageQueu.BeginPeek();
    
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    

    You can see sample code and msmq library on github: https://github.com/beyazc/MsmqInt

    0 讨论(0)
  • 2020-12-15 17:21

    Or you can use

     message.Formatter =
         new System.Messaging.XmlMessageFormatter(new Type[1] { typeof(string) });
    
    0 讨论(0)
  • 2020-12-15 17:21

    this works very fine:

    static readonly XmlMessageFormatter f = new XmlMessageFormatter(new Type[] { typeof(String) });
    
    private void Client()
    {
        var messageQueue = new MessageQueue(@".\Private$\SomeTestName");
    
        foreach (Message message in messageQueue.GetAllMessages())
        {
            message.Formatter = f;
            Console.WriteLine(message.Body);
        }
        messageQueue.Purge();
    }
    
    0 讨论(0)
提交回复
热议问题