2012-05-07 77 views
1

我正在用MassTransit玩一个简单的例子,我不知道我做错了什么,因为我没有收到正确的数据。 我在HomeController的用下面的代码一个简单的asp.net-MVC应用程序Masstransit没有处理正确的值

public class TweetCreatedCommand : Command 
{ 
    public readonly string Message; 
    public readonly DateTime Timestamp; 
    public readonly Guid TweetId; 
    public readonly string Who; 

    public TweetCreatedCommand(Guid tweetId, string message, DateTime timeStamp, string who) 
    { 
     TweetId = tweetId; 
     Message = message; 
     Timestamp = timeStamp; 
     Who = who; 
    } 
} 

public ActionResult Index() 
    { 
     TweetCreatedCommand data; 

     Bus.Initialize(sbc => 
     { 
      sbc.UseMsmq(); 
      sbc.VerifyMsmqConfiguration(); 
      sbc.UseMulticastSubscriptionClient(); 
      sbc.ReceiveFrom("msmq://localhost/test_queue"); 
      sbc.Subscribe(subs => 
      { 
       subs.Handler<TweetCreatedCommand>(msg => data = new TweetCreatedCommand(msg.TweetId, msg.Message,msg.Timestamp,msg.Who)); 
      }); 
     }); 

     Bus.Instance.Publish(new TweetCreatedCommand(Guid.NewGuid(),"foo!",DateTime.Now,"CDA")); 

        ViewData.Model = data; 

     return View(); 
    } 

如果我调试代码,我可以看到TweetCreatedCommand是如何公布,并在MSMQ队列中的数据是确定的,但当处理程序收到数据时: TweetCreatedCommand.TweetId是:00000000-0000-0000-0000-000000000000它应该是其他东西 TweetCreatedCommand.Message为空,它应该是“foo!” TweetCreatedCommand.TimeStamp是01/01/01 谁是空,它应该是“CDA”

什么是错?

任何帮助,将不胜感激

回答

1

MassTransit的默认序列化仅适用于属性。尝试将只读字段更改为属性,并且序列化程序应正确填充数据。如果只需要只读字段,请更改为BinarySerializer(默认为JSON)。

+0

非常感谢你,现在它完美地改变只读字段 – cdiazal

+0

如果我尝试使用UseBinarySerializer它不工作。它在发布时调用TweetCreatedCommand构造函数,但它在发布之后不再调用构造函数,因此变量数据为空。 – cdiazal

+0

将其列入邮件列表:https://groups.google.com/forum/#!forum/masstransit-discuss;我没有任何理由使用过BinarySerializer,我确信周围还有一些额外的陷阱,只是想不到。此外,请查看规格:https://github.com/MassTransit/MassTransit/tree/master/src/MassTransit.Tests/Serialization大部分随后针对每个序列化程序运行。 – Travis