Skip to content

services serialization setup

Brian Greco edited this page Apr 9, 2023 · 1 revision

IMAGE Serialization Setup

NetFusion abstracts out the process of serialization so various implementations can be used. The ISerializationManager service manages a set of serializer classes and determines the one used based on context-type and encoding-type. By default, the SerializationManager registers serializers for JSON and Message Pack. The host application can override, remove, and add serializers. The main use of the serialization manager is when messages(commands and domain-events) are published out of process. This is the case when the Azure Service Bus, RabbitMQ, and Redis plugins are used.


IMAGENote: Based on the serializer used may required the messages to confirm to the requirements of the serializer. The JSON serializer uses Microsoft's System.Text.Json serializer.


Serialization Manager

The class implementing the ISerializationManager interface registers the following IMessageSerializer implementations:

  • JsonMessageSerializer: Delegates to the Microsoft's System.Test.Json serializer.

  • MessagePackSerializer: Delegates to an open source message pack serializer.

The serialization manager determines the serializer based on a requested Content and Encoding types. A lookup based on these values are used to find the matching serializer as follows:

  1. If both the content and encoding types are specified, a lookup to find an exact matching serializer is preformed.
  2. If a serializer is not found, or the encoding type is not specified, a lookup is preformed based only on content type. If exactly one matching serializer is not found, an exception is thrown.
  3. A serializer can also be located based on Web MIME types / Internet Media Types such as: application/json; charset=utf-8. If the charset is not specified, the serializer based on only the content-type is found.

Configuration

By default, NetFusion does not register a default implementation of the ISerializationManager service. The default implementation is provided by the SerializationManager class. The host can override the default implementation completely, or simply alter the default configuration. Most often, an existing serializer will be overridden or a new one added. This is completed by instantiating the SerializationManager class and taking a combination of the following actions:

  • Clear all default registered serializers.
  • Override an existing registered serializer.
  • Register a new serializer.

After the serialization manager is configured, the instance is registered as the ISerializationManager service and overrides the default registration.

Below shows how this is preformed by clearing all message serializers and adding back just the Message Pack serializer:

public class Startup
{
    // Microsoft Abstractions:
    private readonly IConfiguration _configuration;

    public Startup(IConfiguration configuration)
    {
        _configuration = configuration;
    }

    public void ConfigureServices(IServiceCollection services)
    {
        var serializer = new SerializationManager();
        serializer.ClearSerializers();
        serializer.AddSerializer(new MessagePackSerializer());

        services.CompositeContainer(_configuration, new SerilogExtendedLogger())
            .AddSettings()

            .AddPlugin<InfraPlugin>()
            .AddPlugin<AppPlugin>()
            .AddPlugin<DomainPlugin>()
            .AddPlugin<WebApiPlugin>()
            .Compose(overrides => {
                overrides.AddSingleton<ISerializationManager>(serializer);
            });

        services.AddControllers(); 
    }

    // ...
}

Summary

Plugins such as RabbitMQ, AMQP, and Redis will indicate how the content and encoding can be specified to determine how the message is serialized. In all cases, the content and encoding types are stored as dynamic properties on the message. This allows the subscriber to correctly deserialize the message.

Clone this wiki locally