Multiple De-Serializers

Component: NServiceBus | Nuget: NServiceBus (Version: 6.x)

This sample uses the AddDeserializer API to illustrate a receiving endpoint deserializing multiple different serialization formats.

Sending Endpoints

There are multiple sending endpoints, one per serializer.

ExternalNewtonsoftJsonEndpoint

Send messages using the external Json.NET Serializer in Json format.

Edit
var endpointConfiguration = new EndpointConfiguration("Samples.MultipleDeserializers.ExternalNewtonsoftJsonEndpoint");
var serialization = endpointConfiguration.UseSerialization<NewtonsoftSerializer>();
serialization.ContentTypeKey("NewtonsoftJson");
endpointConfiguration.RegisterOutgoingMessageLogger();

ExternalNewtonsoftBsonEndpoint

Send messages using the external Json.NET Serializer in Bson format.

Edit
var endpointConfiguration = new EndpointConfiguration("Samples.MultipleDeserializers.ExternalNewtonsoftBsonEndpoint");
var serialization = endpointConfiguration.UseSerialization<NewtonsoftSerializer>();
serialization.ReaderCreator(stream => new BsonDataReader(stream));
serialization.WriterCreator(stream => new BsonDataWriter(stream));
serialization.ContentTypeKey("NewtonsoftBson");
endpointConfiguration.RegisterOutgoingMessageLogger();

JilEndpoint

Send messages using the Jil Serializer.

Edit
var endpointConfiguration = new EndpointConfiguration("Samples.MultipleDeserializers.JilEndpoint");
var serialization = endpointConfiguration.UseSerialization<JilSerializer>();
serialization.ContentTypeKey("Jil");
endpointConfiguration.RegisterOutgoingMessageLogger();

MergedNewtonsoftEndpoint

Send messages using the merged Json.NET Serializer.

Edit
var endpointConfiguration = new EndpointConfiguration("Samples.MultipleDeserializers.MergedNewtonsoftEndpoint");
endpointConfiguration.UseSerialization<JsonSerializer>();
endpointConfiguration.RegisterOutgoingMessageLogger();

MessagePackEndpoint

Send messages using the MessagePack Serializer.

Edit
var endpointConfiguration = new EndpointConfiguration("Samples.MultipleDeserializers.MessagePackEndpoint");
endpointConfiguration.UseSerialization<MessagePackSerializer>();
endpointConfiguration.RegisterOutgoingMessageLogger();

WireEndpoint

Send messages using the Wire Serializer.

Edit
var endpointConfiguration = new EndpointConfiguration("Samples.MultipleDeserializers.WireEndpoint");
endpointConfiguration.UseSerialization<WireSerializer>();
endpointConfiguration.RegisterOutgoingMessageLogger();

XmlEndpoint

Send messages using the Xml Serializer.

Edit
var endpointConfiguration = new EndpointConfiguration("Samples.MultipleDeserializers.XmlEndpoint");
endpointConfiguration.UseSerialization<XmlSerializer>();
endpointConfiguration.RegisterOutgoingMessageLogger();

Shared

Contains message definitions and a message mutator that logs the outgoing data. Shared by all endpoints and configured by endpointConfiguration.RegisterOutgoingMessageLogger(); in the above snippets.

Edit
class OutgoingMessageBodyWriter :
    IMutateOutgoingTransportMessages
{
    static ILog log = LogManager.GetLogger<OutgoingMessageBodyWriter>();

    public Task MutateOutgoing(MutateOutgoingTransportMessageContext context)
    {
        var bodyAsString = Encoding.UTF8
            .GetString(context.OutgoingBody);
        log.Info($"Serialized Message Body:\r\n{bodyAsString}");
        return Task.CompletedTask;
    }
}

public static class OutgoingMessageBodyWriterHelper
{
    public static void RegisterOutgoingMessageLogger(this EndpointConfiguration endpointConfiguration)
    {
        // register the mutator so the the message on the wire is written
        endpointConfiguration.RegisterComponents(
            registration: components =>
            {
                components.ConfigureComponent<OutgoingMessageBodyWriter>(DependencyLifecycle.InstancePerCall);
            });
    }
}

ReceivingEndpoint

The receiving endpoints references all the serializes used above and adds them using AddDeserializer

Edit
var endpointConfiguration = new EndpointConfiguration("Samples.MultipleDeserializers.ReceivingEndpoint");

// Xml
endpointConfiguration.UseSerialization<XmlSerializer>();

// External Newtonsoft Json
var externalNewtonsoftJson = endpointConfiguration.AddDeserializer<NewtonsoftSerializer>();
externalNewtonsoftJson.ContentTypeKey("NewtonsoftJson");

// External Newtonsoft Bson
var externalNewtonsoftBson = endpointConfiguration.AddDeserializer<NewtonsoftSerializer>();
externalNewtonsoftBson.ReaderCreator(stream => new BsonDataReader(stream));
externalNewtonsoftBson.WriterCreator(stream => new BsonDataWriter(stream));
externalNewtonsoftBson.ContentTypeKey("NewtonsoftBson");

// Jil
var jil = endpointConfiguration.AddDeserializer<JilSerializer>();
jil.ContentTypeKey("Jil");

// Merged Newtonsoft
endpointConfiguration.AddDeserializer<JsonSerializer>();

// Message Pack
endpointConfiguration.AddDeserializer<MessagePackSerializer>();

// Wire
endpointConfiguration.AddDeserializer<WireSerializer>();

// register the mutator so the the message on the wire is written
endpointConfiguration.RegisterComponents(
    registration: components =>
    {
        components.ConfigureComponent<IncomingMessageBodyWriter>(DependencyLifecycle.InstancePerCall);
    });

There is also a message mutator that logs the incoming data.

Edit
public class IncomingMessageBodyWriter :
    IMutateIncomingTransportMessages
{
    static ILog log = LogManager.GetLogger<IncomingMessageBodyWriter>();

    public Task MutateIncoming(MutateIncomingTransportMessageContext context)
    {
        var bodyAsString = Encoding.UTF8
            .GetString(context.Body);
        var contentType = context.Headers[Headers.ContentType];
        log.Info($"ContentType \'{contentType}\'. Serialized Message Body:\r\n{bodyAsString}");
        return Task.CompletedTask;
    }
}

Samples

Related Articles


Last modified