我正在构建一个队列系统,我已经能够将对象发送到位于另一台服务器上的公共队列。我无法弄清楚的是如何在接收器端重建对象(我在两端都有它的定义)。
有什么想法吗?
请查看下面的 MSDN 示例:http://msdn.microsoft.com/en-us/library/y918yfy2(v=vs.110).aspx
基本上,调用 queue.Send(object)
会使用默认 XmlMessageFormatter
序列化对象。因此,您必须使用相同的序列化程序反序列化消息,并将收到的Message.Body
的结果强制转换为良好类型:
// Connect to the a queue on the local computer.
MessageQueue myQueue = new MessageQueue(".\myQueue");
// Set the formatter to indicate body contains an Order.
myQueue.Formatter = new XmlMessageFormatter(new Type[] {typeof(MyProject.Order)});
// Receive and format the message.
Message myMessage = myQueue.Receive();
Order myOrder = (Order)myMessage.Body;