2

Hey im having some problem serializing my POCOs via newtonsoft and rabbitMQ, they both implement IMessage interface, but the POCOs themselves are not shared between the apps, to summarize, i have one IMessage interface and two Message : IMessage classes, one per project, but they have the same structure, which is why im wondering why newtonsoft cant serialize them.

Btw im trying to follow what some folks https://github.com/devmentors have developed here.

The patterns looks like this

pub publishes UserCreated -> rabbitmq bus -> subscriber receives UserCreated but UserCreated are two different classes both implementing IEvent or some other marker interface

subscriber

using System;
using Core;
using Newtonsoft.Json;
using RawRabbit.vNext;

namespace Not
{
    class Sub
    {
        public void Subscribe<TMessage>() where TMessage : IMessage
        {
            var client = BusClientFactory.CreateDefault();
            client.SubscribeAsync<TMessage>(async (message, ctx) =>
                {
                    Console.WriteLine(typeof(Message));
                    Console.WriteLine(message);
                },
                c =>
                {
                    c.WithExchange(q => { q.WithName(typeof(Message).Name); });
                    c.WithRoutingKey("example");
                });
        }
    }
    class Message : IMessage
    {
        [JsonConstructor]
        public Message(string body)
        {
            Body = body;
        }

        public string Body { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Sub");
            var sub = new Sub();
            sub.Subscribe<Message>();
            Console.ReadLine();
        }
    }
}

and my publisher

using System;
using Core;
using Newtonsoft.Json;
using RawRabbit.vNext;

namespace Acceptable
{

    public class Message : IMessage
    {
        public string Body { get; set; }

        [JsonConstructor]
        public Message(string body)
        {
            Body = body;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Pub");
            var client = BusClientFactory.CreateDefault();
            while (true)
            {
                var input = Console.ReadLine();
                var message = new Message(input);
                client.PublishAsync(message, configuration: c =>
                {
                    c.WithRoutingKey(typeof(Message).Name);
                    c.WithExchange(e => e.WithName(typeof(Message).Name));
                });
            }
        }
    }
}

Currently im getting this error https://i.stack.imgur.com/VRuYR.png

'Type specified in JSON 'Acceptable.Message, Acceptable, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not compatible with 'Not.Message, Not, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Path '$type', line 1, position 51.'

and i dont know why theyre not compatible, they both implement IMessage and have the same props.

istrupin
  • 1,423
  • 16
  • 32
ilovekittens
  • 75
  • 1
  • 6

0 Answers0