Getting Started
Architecture
NServiceBus
Transports
Persistence
ServiceInsight
ServicePulse
ServiceControl
Monitoring
Modernization
Samples

System.Text.Json Serializer sample

NuGet Package: NServiceBus (10-pre)
This page targets a pre-release version. Pre-releases are subject to change and samples are not guaranteed to be fully functional.

This sample uses the System.Text.Json serializer built in to NServiceBus to serialize message payloads.

Configuring to use NServiceBus.Newtonsoft.Json

endpointConfiguration.UseSerialization<SystemJsonSerializer>();

Sending the message

var message = new CreateOrder
{
    OrderId = 9,
    Date = DateTime.Now,
    CustomerId = 12,
    OrderItems =
    [
        new OrderItem()
        {
            ItemId = 6,
            Quantity = 2
        },

        new OrderItem()
        {
            ItemId = 5,
            Quantity = 4
        }

    ]
};
await messageSession.SendLocal(message);

Output

{
  "OrderId": 9,
  "Date": "2015-09-15T10:23:44.9367871+10:00",
  "CustomerId": 12,
  "OrderItems": [
    {
      "ItemId": 6,
      "Quantity": 2
    },
    {
      "ItemId": 5,
      "Quantity": 4
    }
  ]
}

Related Articles

  • Serialization
    .NET messaging systems require serialization and deserialization of objects sent/received over transports. NServiceBus achieves this using serializers.
  • System.Text.Json serializer
    A json serializer using System.Text.Json.