【问题标题】:Retrieving object sent through a Message Queue检索通过消息队列发送的对象
【发布时间】:2014-03-29 15:25:39
【问题描述】:

我正在构建一个队列系统,我已经能够将对象发送到位于另一台服务器上的公共队列。 我想不通的是如何在接收端重建对象(我在两端都有它的定义)。

有什么想法吗?

【问题讨论】:

  • 你不能像序列化对象一样反序列化对象以将其放入队列吗?
  • 我没有序列化,只是使用 queue.Send(commObject, "Notification mail");

标签: c# message-queue remoteobject


【解决方案1】:

查看以下 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;

【讨论】:

猜你喜欢
  • 2013-04-02
  • 2012-10-20
  • 2017-04-27
  • 2023-04-10
  • 2018-02-02
  • 1970-01-01
  • 2014-02-01
  • 1970-01-01
  • 2019-02-08
相关资源
最近更新 更多