Skip to content

Event serialisation

Elliot Ritchie edited this page Feb 27, 2014 · 13 revisions

There are a few ways to configure event serialisation when using NES. If you are using NES v3+ the recommended way is to use one of the new custom serialisers, however NES v3+ is backwards compatible with the older method of serialisation if you need it.

NES v3+

NES includes two custom serialisers which allow you to store your events as either Json or Bson. When configuring the EventStore you should call either UsingJsonSerialization() or UsingBsonSerialization() immediately after calling NES(). This allows the use of interfaces for events.

EventStore.Wireup.Init()
    .UsingSqlPersistence("EventStore")
    .InitializeStorageEngine()
    .NES()
    .UsingJsonSerialization()
    .Build();

Currently only Json and Bson serialisation is available.

Raven

If you're using Raven you just need to let NES know that it should customise the built in serialiser. This allows the use of interfaces for events.

EventStore.Wireup.Init()
    .UsingRavenPersistence("EventStore")
    .InitializeStorageEngine()
    .NES()
    .UsingCustomizedRavenSerializer()
    .Build();

NES v2 & v1

When configuring NServiceBus you have the option to specify the type of serialisation. When configuring the EventStore you also have the option to specify the type of serialisation. However these options are actually configuring different things.

NServiceBus.Configure.With()
    .Log4Net()
    .DefaultBuilder()
    .XmlSerializer()
    .NES();

The '.XmlSerializer()' line above is telling NServiceBus to use its XML serialiser to serialise and deserialise NServiceBus.IMessage types.

EventStore.Wireup.Init()
    .UsingSqlPersistence("EventStore")
    .InitializeStorageEngine()
    .UsingJsonSerialization()
    .NES()
    .Build();

The '.UsingJsonSerialization()' line above is telling the EventStore to use its JSON serialiser to serialise and deserialise EventStore.EventMessage types.

It's important to note that NES will respect the configuration choices you make with regards to both of these options. For example if you choose the same configurations as above for your system then EventMessage types will be serialised and persisted as JSON and will contain a 'Body' property that returns an XML string representation of the IMessage.

So why doesn't the IMessage just get JSON serialised as well, why does there have to be a format miss-match? At the time of writing only Binary and XML serialisation is supported by NServiceBus. The XML serialiser is also custom built specifically for dealing with IMessage types and allows the use of interfaces for events. A future enhancement to NServiceBus or NES may mitigate this issue.

Clone this wiki locally