Particular / NServiceBus

Build, version, and monitor better microservices with the most powerful service platform for .NET
https://particular.net/nservicebus/
Other
2.07k stars 650 forks source link

Register multiple serializers for outgoing messages #7070

Open ramonsmits opened 4 weeks ago

ramonsmits commented 4 weeks ago

Is your feature related to a problem? Please describe.

Currently you cannot send a message and use a specific serializer. All outgoing messages are serialized in the same format. For native integration is can be very useful to serialize messages to different endpoints using different formats.

Describe the requested feature

Ability to register multiple SERIALIZERS (so not deserializers as these are already supported) and have methods to use a specific serializer based on convention, a specific message type, custom rule or sendoption.

Convention

However, it might also require a convention as the type could in an assembly now in control of the user.

var serializer = endpointConfiguration.UseSerialization<XmlSerializer>();
serializer.Match(type => type.NameSpace.StartsWith("ServiceX");

SendOption

var sendOptions = new SendOptions();
sendOptions.ContentType("application/json; systemjson");

Attribute

A good candidate would be to have a default serializer as we currently have but have the ability to specify a specific one an attribute:

[ContentType("text/xml")]
public MyCommand
{
}

// Ability to register multiple JSON serializers
[ContentType("application/json; systemjson")]
public MyCommand
{
}

[ContentType("application/json")]
public MyOtherCommand()
{
}

Registrations

var serializer = endpointConfiguration.UseSerialization<XmlSerializer>("text/xml"); // Not specifying would use the default content type key:

It would potentially just be nice if all serializers would be DI registered and can be resolved via a keyed service:

var jsonSerializer = // ..
services.AddKeyedSingleton<IMessageSerializer, JsonMessageSerializer>(jsonSerializer.ConentType);
var xmlSerializer = // ..
services.AddKeyedSingleton<IMessageSerializer, JsonMessageSerializer>(xmlSerializer.ContentType);

Describe alternatives you've considered