简体   繁体   English

邮件正文上的MSMQ空对象

[英]MSMQ empty object on message body

Ok, so I'm very VERY new to MSMQ and I'm already confused. 好的,所以我对MSMQ非常陌生,我已经感到困惑。

I have created a private queue and added a few messages to it, all good so far. 我创建了一个私有队列,并向其中添加了一些消息,到目前为止一切都很好。 BUT when I retrieve the messages back from the queue the message body contains a empty object of the type I added. 但是,当我从队列取回消息时,消息正文包含我添加的类型的空对象。 By this I don't mean that the body is null, it does have a reference to a type of the object that I added, but it's not instantiated so all the properties are in their null or default state. 通过这种方式,我并不是说主体为null,它确实引用了我添加的对象的类型,但是没有实例化,因此所有属性均处于null或默认状态。

This is the code I use to add to the queue: 这是我用来添加到队列中的代码:

using (var mQueue = new MessageQueue(QueueName))
{
    var msg = new Message(observation)
    {
            Priority = MessagePriority.Normal,
             UseJournalQueue = true,
            AcknowledgeType = AcknowledgeTypes.FullReceive,
    };
    mQueue.Send(msg);
}

And this is the code that dequeues the messages: 这是使消息出队的代码:

using (var mQueue = new MessageQueue(QueueName))
{
    mQueue.MessageReadPropertyFilter.SetAll();
    ((XmlMessageFormatter)mQueue.Formatter).TargetTypes =
                                                  new[] { typeof(Observation) };
    var msg = mQueue.Receive(new TimeSpan(0, 0, 5));
    var observation = (Observation)msg.Body;

    return observation;
}

The Message constructor uses XML serialization to serialize your "observation" object. Message构造函数使用XML序列化来序列化“观察”对象。 You'll need to make sure that this works properly. 您需要确保它可以正常运行。 XML serialization will only deal with public members of the class, it is not going to serialize private members. XML序列化只会处理类的公共成员,而不会序列化私有成员。 Your object may well look "empty" after it is deserialized again. 再次反序列化后,您的对象可能看起来很“空”。

Here's some test code to verify that it works properly: 这是一些测试代码,以验证其是否正常运行:

using System;
using System.IO;
using System.Xml.Serialization;

class Program {
  static void Main(string[] args) {
    var ser = new XmlSerializer(typeof(Observation));
    var sw = new StringWriter();
    var obj = new Observation();
    ser.Serialize(sw, obj);
    Console.WriteLine(sw.ToString());
    var sr = new StringReader(sw.ToString());
    var obj2 = (Observation)ser.Deserialize(sr);
    // Compare obj to obj2 here
    //...
    Console.ReadLine();
  }
}
public class Observation {
  // etc...
}

另外,请确保您的自定义Message对象在每个属性上都有公共setters

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM