pardahlman / RawRabbit

A modern .NET framework for communication over RabbitMq
MIT License
747 stars 144 forks source link

Unable to send message if class has multiple constructors #316

Closed arvigeus closed 6 years ago

arvigeus commented 6 years ago

RawRabbit 2.0-rc4 This is very peculiar: If a class has more than one constructor, sending it as a message does not work, unless one of the constructors is default one. I am using RawRabbit.Enrichers.Attributes Example (see comments):

[Exchange(Name = "service", Type = ExchangeType.Topic)]
[Queue(Name = "notification", MessageTtl = 3000)]
[Routing(RoutingKey = "notification", AutoAck = true, PrefetchCount = 50)]
class BasicMessage
{
    public BasicMessage(string message) { }

    // Uncomment this to work
    //public BasicMessage() { }
    // Or comment out this to work
    public BasicMessage(string message1, string message2) { }
}

Full code for reference

pardahlman commented 6 years ago

Hi @arvigeus, RawRabbit uses Newtonsoft's JSON serializer out of the box. This serializser can not handle non-default constructors out of the box. There are a few ways to work around this, including adding constructor with attribute [JsonConstructor] or a specific Converter that understands how to create an instance of the message. Perhaps these links might be helpful:

arvigeus commented 6 years ago

This is kinda bummer because nothing communicates where is the problem. Should I close the issue now? Is there nothing that can be done about it to prevent confusion?

pardahlman commented 6 years ago

If you think about it, it makes sense that a serialization framework that takes a string and creates an instance of a type can't construct the type if the constructor requires argument and it doesn't know how to provide them. I'm closing this issue, as it is related to newtonsoft rather than RawRabbit. If you want, you can look at the other serialization options (RawRabbit currently supports Protobuf, ZeroFormat and MessagePack) and see if they fit your needs better.

Cheers!