Custom Serilializer for DataBus

Component: NServiceBus
NuGet Package NServiceBus (7-pre)
This page targets a pre-release version and is subject to change prior to the final release.
  1. Run the solution. Two console applications start.
  2. Find the Sender application by looking for the one with "Sender" in it's console title and press Enter to send a message. A message is sent that includes a DataBus property that is serialized using a custom Json serializer.
  3. The sample uses the FileShareDataBus. Open the Solution Folder in Windows Explorer and navigate to the \storage\ sub-folder. Each sub-folder within the \storage folder contains serialized DataBus properties.
The FileShareDataBus does not remove physical attachments once the message has been processed. Apply a custom cleanup-strategy.

Code walk-through

This sample contains three projects:

  • Shared - A class library containing the sample messages and the custom DataBus serilaizer.
  • Sender - A console application responsible for sending the large messages.
  • Receiver - A console application responsible for receiving the large messages from the sender.

Shared project

Look at the Shared project, at the custom DataBus serializer.

public class JsonDataBusSerializer :
    IDataBusSerializer
{
    public void Serialize(object databusProperty, Stream stream)
    {
        using (var writer = CreateNonClosingStreamWriter(stream))
        using (var jsonWriter = new JsonTextWriter(writer))
        {
            serializer.Serialize(jsonWriter, databusProperty);
        }
    }

    StreamWriter CreateNonClosingStreamWriter(Stream stream)
        => new StreamWriter(
            stream,
            Encoding.UTF8,
            bufferSize: 1024,
            leaveOpen: true);

    public object Deserialize(Stream stream)
    {
        using (var reader = new StreamReader(stream))
        using (var jsonReader = new JsonTextReader(reader))
        {
            return serializer.Deserialize(jsonReader);
        }
    }

    JsonSerializer serializer = JsonSerializer.Create(
        new JsonSerializerSettings
        {
            TypeNameHandling = TypeNameHandling.All
        }
    );
}

The custom serializer implements IDataBusSerializer.

Sender project

The endpoint in the Sender project is configured to use the custom DataBus serializer:

endpointConfiguration.RegisterComponents(
    registration: components =>
    {
        components.ConfigureComponent<JsonDataBusSerializer>(DependencyLifecycle.SingleInstance);
    });

var dataBus = endpointConfiguration.UseDataBus<FileShareDataBus>();
dataBus.BasePath("..\\..\\..\\storage");

Receiver project

The endpoint in the Receiver project must be configured to use the same custom DataBus serializer in order to read DataBus properties sent by the Sender endpoint:

endpointConfiguration.RegisterComponents(
    registration: components =>
    {
        components.ConfigureComponent<JsonDataBusSerializer>(DependencyLifecycle.SingleInstance);
    });

var dataBus = endpointConfiguration.UseDataBus<FileShareDataBus>();
dataBus.BasePath("..\\..\\..\\storage");

Related Articles


Last modified