Getting Started
Architecture
NServiceBus
Transports
Persistence
ServiceInsight
ServicePulse
ServiceControl
Monitoring
Samples

Multiple Deserializers

Component: NServiceBus
NuGet Package: NServiceBus (8.x)

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

Sending endpoints

There are multiple sending endpoints, one per serializer.

NewtonsoftJsonEndpoint

Sends messages using the external Json.NET serializer in JSON format.

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

NewtonsoftBsonEndpoint

Sends messages using the external Json.NET serializer in BSON format.

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

var serialization = endpointConfiguration.UseSerialization<NewtonsoftJsonSerializer>();

serialization.ReaderCreator(stream => new BsonDataReader(stream));
serialization.WriterCreator(stream => new BsonDataWriter(stream));
serialization.ContentTypeKey("NewtonsoftBson");

endpointConfiguration.RegisterOutgoingMessageLogger();

XmlEndpoint

Sends messages using the XML serializer.

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

SystemJsonEndpoint

Sends messages using System.Text.Json in JSON format.

var endpointConfiguration = new EndpointConfiguration("Samples.MultipleDeserializers.SystemJsonEndpoint");
var serialization = endpointConfiguration.UseSerialization<SystemJsonSerializer>();

endpointConfiguration.RegisterOutgoingMessageLogger();

Shared

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

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

    public Task MutateOutgoing(MutateOutgoingTransportMessageContext context)
    {
        var bodyAsString = Encoding.UTF8
            .GetString(context.OutgoingBody.ToArray());
        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.RegisterMessageMutator(new OutgoingMessageBodyWriter());
    }
}

ReceivingEndpoint

The receiving endpoint references all the serializers used above and adds them using AddDeserializer

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

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

// Json
endpointConfiguration.AddDeserializer<SystemJsonSerializer>();

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

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

// register the mutator so the the message on the wire is written
endpointConfiguration.RegisterMessageMutator(new IncomingMessageBodyWriter());

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

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

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

Related Articles

  • Serialization
    .NET messaging systems require serialization and deserialization of objects sent/received over transports. NServiceBus achieves this using serializers.