0

鉴于此事件

 public class DummyEvent : EventDTO{
    public CustomUUID Cid { get; set; }
    public DateTime Date { get; set; }
    public Guid Id { get; set; }
}

我通过 MSMQ 发送消息:

 var de = new DummyEvent {
                               Date = DateTime.UtcNow, 
                               Id = Guid.NewGuid(), 
                               Cid = Guid.NewGuid()
                           };
 var mq = new MessageQueue(AppSettings.EventQueue);                            
 mq.Formatter = new XmlMessageFormatter(new[]{de.GetType()});
 mq.Send(de);
 var e = reciever.Receive().Body;

Date 是System.DateTime所以它可以反序列
Id 是System.Guid,所以它可以反序列
Cid 是CustomUUID,它是用户定义的类型,不会被反序列化。

我还需要发送客户定义的类型,但网上信息很少。

编辑:它实际上没有被序列化,所以问题在于发送消息

4

1 回答 1

2

这是示例

using System;
using System.Messaging;

namespace MsmqSample
{
    internal class Program
    {
        private static MessageQueue _queue;
        private static readonly BinaryMessageFormatter _formatter = new BinaryMessageFormatter();

        private static void Main(string[] args)
        {
            const string queueName = @".\private$\TaskQueue";
            if (!MessageQueue.Exists(queueName))
            {
                MessageQueue.Create(queueName, true);
            }
            _queue = new MessageQueue(queueName);
            _queue.Purge();
            AddMessage();
            Console.ReadKey();
        }

        private static void AddMessage()
        {
            using (var transaction = new MessageQueueTransaction())
            {
                try
                {
                    transaction.Begin();
                    var externalMessage = new ExternalMessage {Data = "Test Data"};
                    _queue.Send(new Message(externalMessage, _formatter), transaction);
                    transaction.Commit();
                }
                catch (Exception)
                {
                    transaction.Abort();
                }
            }
        }
    }

    [Serializable]
    public class ExternalMessage
    {
        public string Data;

        public ExternalMessage()
        {
            Content = new MessageContent {Guid = Guid.NewGuid()};
        }

        public MessageContent Content { get; set; }
    }

    [Serializable]
    public class MessageContent
    {
        public Guid Guid { get; set; }
    }
}
于 2012-07-30T18:35:36.430 回答