Skip to content

Message compression and encryption

Mogens Heller Grabe edited this page Jan 26, 2014 · 5 revisions

Serialization

By default, Rebus uses the built-in JSON serializer which internally uses the excellent Newtonsoft JSON.NET. The Newtonsoft DLL is Ilmerged into Rebus, so as to not bother you by that dependency.

Message encryption

Rebus supports automatic encryption of message bodies, which means that the contents of messages cannot easily be read by eavesdroppers. This can be useful if you're e.g. sending messages containing payroll information, or other kinds of information that should not be readily readable.

You can enable encryption by going:

Configure.With(...)
    .(...)
    .Decorators(d => d.EncryptMessageBodies("base 64 encoded encryption key"))
    .(...)

or

Configure.With(...)
    .(...)
    .Decorators(d => d.EncryptMessageBodies())
    .(...)

to pick up the key from the current app.config. In case you forget to provide a valid key, Rebus is nice enough to generate one for you that you may copy into your configuration file and start using :) Rebus will used the built-in functionality in .NET to generate the key.

Message compression

Since many serialization formats are pretty verbose, and JSON in that regard is probabably one of the most verbose formats because it includes the entire schema along with every single message, you may want to enable message compression.

In order to enable compression, you can do this:

Configure.With(...)
    .(...)
    .Decorators(d => d.CompressMessageBodies(512))
    .(...)

in order to compress messages whose raw byte[] payload exceeds 512 bytes in length, or

Configure.With(...)
    .(...)
    .Decorators(d => d.CompressMessageBodies())
    .(...)

in order to use the default threshold of 2048 bytes.

Encryption and compression combined

You can, of course, combine both encryption and compression, by going

Configure.With(...)
    .(...)
    .Decorators(d => {
        d.EncryptMessageBodies();
        d.CompressMessageBodies();
    })
    .(...)

Since encryption scrambles messages badly, Rebus will of course compress messages before they're encrypted when sending, and in reverse upon receiving them, no matter how you configure it.

Clone this wiki locally