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

前端 未结 9 1962
耶瑟儿~
耶瑟儿~ 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(MqReceived 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

提交回复
热议问题