pardahlman / RawRabbit

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

How do I publish raw Json messages ? #398

Open iniside opened 5 years ago

iniside commented 5 years ago

Is it possible to publish raw Json Messages ? Some context. I have client apps, which connect to gateway service over websocket connection. Since clients can be in variety of from and languages I decided that best course of action would be to send messages as Json and then on gateway publish them to Rabbit.

I would like to avoid for gateway to know type of messages if possible. I just want it to act as dumb router and connection load balancer not message translation from clients.

I tried to find in docs and here but I couldn't find any example of how (and if possible) to do it, or how the Json message should be formatted.

rpawlaszek commented 5 years ago

Is it possible to publish raw Json Messages ?

Yes. This is set by default.

Ok, there is one thing, this setting will include $id property in the serialized messages, so if you want to prevent that you need to update the di. To do that specify a new ISerializer e.g. like this:

new RawRabbitOptions()
{
    ClientConfiguration = ...,
    DependencyInjection = di => di.AddSingleton<ISerializer, RawRabbit.Serialization.JsonSerializer>(resolver => new RawRabbit.Serialization.JsonSerializer(new Newtonsoft.Json.JsonSerializer
    {
    TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Simple,
    Formatting = Formatting.None,
    CheckAdditionalContent = true,
    ContractResolver = new DefaultContractResolver { NamingStrategy = new CamelCaseNamingStrategy() },
    ObjectCreationHandling = ObjectCreationHandling.Auto,
    DefaultValueHandling = DefaultValueHandling.Ignore,
    TypeNameHandling = TypeNameHandling.Auto,
    ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
    MissingMemberHandling = MissingMemberHandling.Ignore,
    PreserveReferencesHandling = PreserveReferencesHandling.None,
    NullValueHandling = NullValueHandling.Ignore
    }))
}

The only change with respect to the original is PreserveReferencesHandling = PreserveReferencesHandling.None (see here for explanation).