Masstransit Not Handling Correct Values
-
06-06-2021 - |
Question
I´m playing with MassTransit with a simple example and I don´t know what I´m doing wrong, because I don´t receive the correct data. I have a simple asp.net-mvc app with the following code in the HomeController
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();
}
If I debug this code, I can see how the TweetCreatedCommand is published and the data is OK in the MSMQ queue, but when the handler receives the data: TweetCreatedCommand.TweetId is: 00000000-0000-0000-0000-000000000000 and it should be other thing TweetCreatedCommand.Message is null and it should be "foo!" TweetCreatedCommand.TimeStamp is 01/01/01 Who is null and it should be "CDA"
What is wrong??
Any help would be appreciated
Solution
MassTransit's default serialization only works on properties. Try changing your readonly fields to properties and the serializer should populate the data correctly. If readonly fields are required, change to the BinarySerializer
(default is JSON).