开发者

unable to read serialized data as message body in msmq c# 3.0

开发者 https://www.devze.com 2022-12-27 08:53 出处:网络
This is my method to send message to a private Q using (MessageQueue msgQ = new MessageQueue(MessageQueueName))

This is my method to send message to a private Q

using (MessageQueue msgQ = new MessageQueue(MessageQueueName))
        {
            using (System.Messaging.Message newMessage = new System.Messaging.Message(MessageBody,
                new System.Messaging.ActiveXMessageFormatter()))
            {
                newMessage.Label = MessageLabel;
                newMessage.Priority = Priority;
                msgQ.Send(newMessage);
            }

        }

I have an order object which i serialize and send as message body. The serialized object is

<?xml version="1.0"?>
<OrderInfo>
    <OrderID>11111</OrderID>
    <OrderDetails>
        <LineItem>
            <ProductDetails>
                <Name>qwqwqw</Name>
                <Manufacturer>asasas</Manufacturer>
                <UPC>12222222222</UPC>
                <sku>2132</sku>
                <Price>12.21</Price>
            </ProductDetails>
            &开发者_如何学JAVAlt;Quantity>1</Quantity>
        </LineItem>
    </OrderDetails>
</OrderInfo>

This is my method to receive that message in a windows service

 void queue_ReceiveCompleted(object sender, ReceiveCompletedEventArgs asyncResult)
{
    // Connect to the queue.
    MessageQueue mq = (MessageQueue)sender;

    // End the asynchronous Receive operation.
    Message m = mq.EndReceive(asyncResult.AsyncResult);

    m.Formatter = new System.Messaging.ActiveXMessageFormatter()

    //Get Filedata from body
    OrdrInfo qMessage = (OrdrInfo)XMLUtil.Deserialize(m.Body.ToString(), typeof(OrdrInfo));

}

when I try to look at m.Body in quickwatch this is what it states

m.Body.Message = Cannot find a formatter capable of reading this message.

m.Body.StackTrace = at System.Messaging.Message.get_Body()


Hopefully you're not still stuck on this, but as it came up top of my search when running into the same problem.

As no one had answered it, here is one answer that I've just found else where (thanks TechRepublic). This code assume that "MyType" is a typically basic message that can be read by XML Serialisation - this means it is marked as serializable and all data to be sent/reconstructed is in public get/set properties.

Code is:

MessageQueue msgQ = new MessageQueue(@".\private$\CreateNewEntity");

msgQ.Formatter = new XmlMessageFormatter(new []{typeof(MyType)});            
var msg = msgQ.Receive();

msgQ.Close();
return msg.Body as MyType;
0

精彩评论

暂无评论...
验证码 换一张
取 消