Skip to content

ProtoBuf Serializer

Damian Maclennan edited this page Jan 20, 2015 · 1 revision

First thing we're going to need to install the package for the ProtoBuf serializer:

Install-Package Nimbus.Serializers.Protobuf

Now we can go ahead and configure Nimbus as usual:

var connectionString = ConfigurationManager.AppSettings["AzureConnectionString"];

var typeProvider = new AssemblyScanningTypeProvider(Assembly.GetExecutingAssembly());

var messageHandlerFactory = new DefaultMessageHandlerFactory(typeProvider);

var bus = new BusBuilder()
    .Configure()
    .WithNames("MyApplication", Environment.MachineName)
    .WithConnectionString(connectionString)
    .WithTypesFrom(typeProvider)
    .WithDefaultHandlerFactory(messageHandlerFactory)
    .WithDefaultTimeout(TimeSpan.FromSeconds(10))
    .WithProtoBufSerializer()
    .Build();
bus.Start();

return bus;

The main thing to notice there is the ".WithProtoBufSerializer()". This is all you need for setup.

Type Configuration

You'll need to make sure you configure your classes properly. You should read more into ProtoBuf but here's some basics to get you started:

[ProtoContract]
public class Person : IBusCommand
{
    [ProtoMember(1)]
    public string Message {get;set;}
}

Or if you hate attributes and you'd prefer you can configure the serializer at runtime:

var personDef = RuntimeTypeModel.Default.Add(typeof (Person), false);
personDef.Add(1, "Message");