ProtoBuf-Google Serializer Usage

Component: ProtoBuf-Google Serializer
NuGet Package NServiceBus.ProtoBufGoogle (2-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 ProtoBuf

var endpointConfiguration = new EndpointConfiguration("Samples.Serialization.ProtoBufGoogle");
endpointConfiguration.UseSerialization<ProtoBufGoogleSerializer>();

The message send

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

The message definition

The sample uses a .proto file to generate the message contract.

syntax = "proto3";
message CreateOrder {
   int32 OrderId = 1;
   int32 CustomerId = 2;
   repeated OrderItem OrderItems = 3;
}
message OrderItem {
   int32 ItemId = 1;
   int32 Quantity = 2;
}

This is done using protoc.exe

protoc.exe -I=C:\Code\Sample --csharp_out=C:\Code\Sample C:\Code\Sample\CreateOrder.proto

With the resultant class definitions residing in CreateOrder.cs

To mark the generated class as an NServiceBus IMessage a partial class is used.

public partial class CreateOrder : NServiceBus.IMessage
{
}

More Info

For more information on Google Protocol Buffers see

Related Articles


Last modified