Noobie3001
Noobie3001

Reputation: 1251

EasynetQ bus.Subscribe not firing - throwing "messageType must not be null"

I can successfully publish a message with the following code:

        using (IAdvancedBus bus = RabbitHutch.CreateBus("host=192.168.153.128:5672;username=user;password=pass").Advanced)
        {
            IExchange exchange = bus.ExchangeDeclare("milk.api", ExchangeType.Topic);
            IQueue queue = bus.QueueDeclare("milkorderqueue");
            IBinding binding = bus.Bind(exchange, queue, "#");
            bus.Publish<MilkOrderRequest>(exchange, "#", true, new Message<MilkOrderRequest>(milkOrder));
        }
        return new HttpResponseMessage(System.Net.HttpStatusCode.OK);

When my subscriber tries to pick up the message, the EasyNetQ_Default_Error_Queue queue increments. Here is the code for the listener:

        IBus bus = RabbitHutch.CreateBus(@"host=192.168.153.128:5672;username=user;password=pass");
        bus.Subscribe<MilkOrderRequest>("my_subscription_id", message =>
        {
            Console.WriteLine($"Order recieved: {message.Milk}.");
        }, config =>
        {
            config.WithQueueName("milkorderqueue");
        });

I've also tried the following with no success:

        IAdvancedBus bus = RabbitHutch.CreateBus(@"host=192.168.153.128:5672;username=user;password=pass").Advanced;
        IExchange exchange = bus.ExchangeDeclare("milkshop.subscriber", ExchangeType.Topic);
        IQueue queue = bus.QueueDeclare("milkorderqueue");
        bus.Bind(exchange, queue, "#");
        bus.Consume<MilkOrderRequest>(queue, (message, info) =>
        {
            Console.WriteLine($"Order recieved: {message.Body.Milk}.");
        });

I'm having trouble finding answers on Google about the exception:

messageType must not be null

Here is the full message:

{
   "RoutingKey":"#",
   "Exchange":"milk.api",
   "Queue":"milkorderqueue",
   "Exception":"System.AggregateException: One or more errors occurred. (messageType must not be null\r\nParameter name: messageType) ---> System.ArgumentNullException: messageType must not be null\r\nParameter name: messageType\r\n   at EasyNetQ.Preconditions.CheckNotNull[T](T value, String name, String message)\r\n   at EasyNetQ.Preconditions.CheckNotNull[T](T value, String name)\r\n   at EasyNetQ.MessageFactory.CreateInstance(Type messageType, Object body, MessageProperties properties)\r\n   at EasyNetQ.RabbitAdvancedBus.<>c__DisplayClass17_0.<Consume>b__0(Byte[] body, MessageProperties properties, MessageReceivedInfo messageReceivedInfo)\r\n   at EasyNetQ.Consumer.HandlerRunner.InvokeUserMessageHandler(ConsumerExecutionContext context)\r\n   --- End of inner exception stack trace ---\r\n---> (Inner Exception #0) System.ArgumentNullException: messageType must not be null\r\nParameter name: messageType\r\n   at EasyNetQ.Preconditions.CheckNotNull[T](T value, String name, String message)\r\n   at EasyNetQ.Preconditions.CheckNotNull[T](T value, String name)\r\n   at EasyNetQ.MessageFactory.CreateInstance(Type messageType, Object body, MessageProperties properties)\r\n   at EasyNetQ.RabbitAdvancedBus.<>c__DisplayClass17_0.<Consume>b__0(Byte[] body, MessageProperties properties, MessageReceivedInfo messageReceivedInfo)\r\n   at EasyNetQ.Consumer.HandlerRunner.InvokeUserMessageHandler(ConsumerExecutionContext context)<---\r\n",
   "Message":"{\"Milk\":\"semi-skimmed\"}",
   "DateTime":"2018-06-01T11:01:11.645241Z",
   "BasicProperties":{
      "ContentType":null,
      "ContentEncoding":null,
      "Headers":{

      },
      "DeliveryMode":0,
      "Priority":0,
      "CorrelationId":"fe1561c1-4c7c-4489-857e-b664f27a5e5f",
      "ReplyTo":null,
      "Expiration":null,
      "MessageId":null,
      "Timestamp":0,
      "Type":"Milkshop.Common.MilkOrderRequest:Milkshop.Common",
      "UserId":null,
      "AppId":null,
      "ClusterId":null,
      "ContentTypePresent":false,
      "ContentEncodingPresent":false,
      "HeadersPresent":true,
      "DeliveryModePresent":false,
      "PriorityPresent":false,
      "CorrelationIdPresent":true,
      "ReplyToPresent":false,
      "ExpirationPresent":false,
      "MessageIdPresent":false,
      "TimestampPresent":false,
      "TypePresent":true,
      "UserIdPresent":false,
      "AppIdPresent":false,
      "ClusterIdPresent":false
   }
}

I'm using EasynetQ version 3.0.0 on .Net Core applications

Upvotes: 2

Views: 4315

Answers (4)

TheRock
TheRock

Reputation: 1541

In my case the issue had to do with the publisher service using an older version of ENQ which in turn was using the LegacyTypeNameSerializer. On the consuming service side which was using a newer version of ENQ all I had to do was add the following when creating the Bus:

serviceRegister.EnableLegacyTypeNaming();

It's an extension method that should be included in the newer versions of ENQ.

Upvotes: 0

Allan Zeidler
Allan Zeidler

Reputation: 337

I have the same problem, but in my case I was trying to publish a message on RabbitMQ Management console and consume on my application. It does not work.

Try to do another application (I did another method to simulate the publish on the same app) with same EasyNetQ version and use Publish method instead.

Upvotes: 1

Enrique Ramos
Enrique Ramos

Reputation: 36

I don't know if you still facing the issue, I found myself struggling with it for at least 3 days, it seems is an issue with the DefaultMessageSerializationStrategy, for Netcore2. I was able to fix using this custom classes I found on internet:

  • JsonSerializer (Implements: ISerializer)
  • TypeNameSerializer (Implements: ITypeNameSerializer)

Finally you can create your own instance of DefaultMessageSerializationStrategy and using DI:

r.Register<IMessageSerializationStrategy>(new DefaultMessageSerializationStrategy(new TypeNameSerializer(), new MyJsonSerializer(new TypeNameSerializer()), new DefaultCorrelationIdGenerationStrategy()));

Is not the best solution but did the trick :D PD: I don't have the links that lead me to the implementations above but surely with a you'll find them with couple of searches on internet.

Upvotes: 1

Noobie3001
Noobie3001

Reputation: 1251

Found the error. My API project and Console app were using different versions of EasynetQ nuget package. One was 2.33 and the other was 3.0.0.

Upvotes: 3

Related Questions