Jil Serializer Usage

Component: Jil Serializer
NuGet Package NServiceBus.Jil (2.1-pre)
This is a community run project
Target NServiceBus Version: 7.x
This page targets a pre-release version and is subject to change prior to the final release.

Configuring to use Jil

var endpointConfiguration = new EndpointConfiguration("Samples.Serialization.Jil");
endpointConfiguration.UseSerialization<JilSerializer>()
    .Options(
        new Options(
            prettyPrint: true,
            excludeNulls: true,
            includeInherited: true));
// register the mutator so the the message on the wire is written
endpointConfiguration.RegisterComponents(components =>
{
    components.ConfigureComponent<MessageBodyWriter>(DependencyLifecycle.InstancePerCall);
});

Diagnostic Mutator

A helper that will Write out the contents of any incoming message.

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

    public Task MutateIncoming(MutateIncomingTransportMessageContext context)
    {
        var bodyAsString = Encoding.UTF8
            .GetString(context.Body);
        log.Info("Serialized Message Body:");
        log.Info(bodyAsString);
        return Task.CompletedTask;
    }
}

The message send

var message = new CreateOrder
{
    OrderId = 9,
    Date = DateTime.Now,
    CustomerId = 12,
    OrderItems = new List<OrderItem>
    {
        new OrderItem
        {
            ItemId = 6,
            Quantity = 2
        },
        new OrderItem
        {
            ItemId = 5,
            Quantity = 4
        },
    }
};
await endpointInstance.SendLocal(message)
    .ConfigureAwait(false);

The Output

{
 "Date": "\/Date(1442274949826)\/",
 "CustomerId": 12,
 "OrderId": 9,
 "OrderItems": [{
  "Quantity": 2,
  "ItemId": 6
 }, {
  "Quantity": 4,
  "ItemId": 5
 }]
}

Related Articles


Last modified