2014-02-26 53 views
0

我构建了一个队列系统,并且能够将对象发送到位于另一个服务器上的公共队列。 我无法弄清楚的是如何在接收端重建对象(我在两端都有它的定义)。检索通过消息队列发送的对象

任何想法?

+0

你能不能反序列化对象的序列化,你是把它以同样的方式在队列中? – ken2k

+0

我不序列化它,只是使用queue.Send(commObject,“Notification mail”); – MaPi

回答

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; 
+0

如果我有不同格式的信息会怎么样? – MaPi

+1

@ user1648371您可以指定多种类型,请参阅http://stackoverflow.com/a/10798997/870604 – ken2k