MassTransit: Problem with MassTransit deserialization

Okay so I have a problem with using MassTransit and it’s deserialization.

We’re publishing messages into a specific queue with RabbitMQ, which should then be further processed by our background services using MassTransit.

The string coming in through the queue looks as following

{

  "routingKey": "article.put",

  "responseQueue": {

    "exchange": "testexchange_gateway",

    "responseRoutingKey": "gatewayResponseQueue",

    "vhost": "testvhost"

  },

  "deviceProperties": {

    "deviceType": "DESKTOP",

    "deviceSize": {

      "width": 10,

      "height": 10

    },

    "javascriptEnabled": false

  },

  "action": "PUT",

  "transactionId": "62b6174d-04b7-4937-b617-4d04b70937b0",

  "contentType": "application/json",

  "body": "Testmessage published while running"

}

With the Contract looking like

public interface IMessage
   {
       string routingKey { get; set; }
       ResponseQueue responseQueue { get; set; }
       DeviceProperties deviceProperties { get; set; }
       string action { get; set; }
       string transactionId { get; set; }
       string contentType { get; set; }
       string body { get; set; }
   }
public interface ResponseQueue
    {
        string exchange { get; set; }
        string responseRoutingKey { get; set; }
        string vhost { get; set; }
    }
 public interface DeviceProperties
    {
        string deviceType { get; set; }
        DeviceSize deviceSize { get; set; }
        bool javascriptEnabled { get; set; }

    }

    public interface DeviceSize
    {
        int width { get; set; }
        int height { get; set; }
    }

My problem now is, whenever I want to consume that message using MassTransit, I will get an error in my _error queue:

MT-Fault-ExceptionType: | System.ArgumentNullException
-- | --
Value cannot be null. Parameter name: source
at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source) at MassTransit.Serialization.JsonConsumeContext..ctor(JsonSerializer deserializer, IObjectTypeDeserializer objectTypeDeserializer, ReceiveContext receiveContext, MessageEnvelope envelope) at MassTransit.Serialization.JsonMessageDeserializer.MassTransit.IMessageDeserializer.Deserialize(ReceiveContext receiveContext)

which I don’t quite understand, as all values needed should be right there.

My consumer:

class ArticleGetConsumer : IConsumer<IMessage>
    {
        public Task Consume(ConsumeContext<IMessage> context)
        {
            var routingKey = context.Message.routingKey;
            return Task.FromResult(0);
        }
    }

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 15 (8 by maintainers)

Most upvoted comments

Should look like this:

{
  "destinationAddress": "rabbitmq://localhost/input_queue",
  "headers": {},
  "messageType": [
    "urn:message:YourProject.Messages:IMessage"
    ],
  "message": {
    "routingKey": "article.put",
    "responseQueue": {
      "exchange": "testexchange_gateway",
      "responseRoutingKey": "gatewayResponseQueue",
      "vhost": "testvhost"
    },
    "deviceProperties": {
      "deviceType": "DESKTOP",
      "deviceSize": {
        "width": 10,
        "height": 10
      },
      "javascriptEnabled": false
    },
    "action": "PUT",
    "transactionId": "62b6174d-04b7-4937-b617-4d04b70937b0",
    "contentType": "application/json",
    "body": "Testmessage published while running"
  }
}

Sounds like you need to use the RawJsonMessageDeserializer.

Covered in this video.

You need to wrap that message in an envelope. The format is detailed in the interoperability documentation:

http://masstransit-project.com/MassTransit/advanced/interoperability.html

Updated URL https://masstransit-project.com/architecture/interoperability.html

Yes, you can write your own deserializer - there isn’t anything to fix in MT with this issue.

Hello,

My issue is the same. The messages are published by python code which publishes the plain application level message with no mass transit specific attributes in the json. Is there a way to still consume the messages from the queue, if published by a non mass transit type client? I get a serialization error : StackTrace: | at System.Linq.Enumerable.ToArray[TSource](IEnumerable1 source) at MassTransit.Serialization.JsonConsumeContext..ctor(JsonSerializer deserializer, IObjectTypeDeserializer objectTypeDeserializer, ReceiveContext receiveContext, MessageEnvelope envelope) at MassTransit.Serialization.JsonMessageDeserializer.MassTransit.IMessageDeserializer.Deserialize(ReceiveContext receiveContext)

Thanks a lot

You need to wrap that message in an envelope. The format is detailed in the interoperability documentation:

http://masstransit-project.com/MassTransit/advanced/interoperability.html