AI agents: use the documentation index at llms.txt to locate machine-readable pages. This section is indexed by https://docs.particular.net/samples/llms.txt. The markdown version of this page is served as plain text. An MCP server at /mcp serves the same content via the search_docs and read_doc tools; it is read-only and needs no credentials. Markdown versions of documentation pages are available by appending .md to the page URL. Directory URLs use index.md. They are served as text/plain because some retrieval backends reject text/markdown.

Message body encryption

Component:
NServiceBus
NuGet Package:
NServiceBus 10.x

This sample demonstrates how to use IMutateTransportMessages to encrypt and decrypt the binary representation of a message as it passes through the pipeline.

Running the solution starts two console applications. Endpoint1 encrypts a message and sends it and Endpoint2 receives the encrypted message and decrypts it.

Endpoint1 output

Message sent.

Endpoint2 output

CompleteOrderHandler Received CompleteOrder with credit card number 123-456-789

Code walk-through

Message contract

The Shared project contains CompleteOrder.cs, which defines the message contract:

public class CompleteOrder : IMessage
{
    public string CreditCard { get; set; }
}

Note that a custom property type is not required.

Encryption configuration

Encryption is enabled by calling an extension method in Program.cs in both Endpoint1 and Endpoint2:

endpointConfiguration.RegisterMessageEncryptor();

The extension method is in Shared/EncryptionExtensions.cs:

public static class EndpointConfigurationExtensions
{
    public static void RegisterMessageEncryptor(this EndpointConfiguration endpointConfiguration)
    {
        endpointConfiguration.RegisterMessageMutator(new MessageEncryptor());
    }
}

The mutator is in Shared/MessageEncryptor.cs:

public class MessageEncryptor :
    IMutateIncomingTransportMessages,
    IMutateOutgoingTransportMessages
{

    public Task MutateIncoming(MutateIncomingTransportMessageContext context)
    {
        context.Body = context.Body.ToArray().Reverse().ToArray();
        return Task.CompletedTask;
    }

    public Task MutateOutgoing(MutateOutgoingTransportMessageContext context)
    {
        context.OutgoingBody = context.OutgoingBody.ToArray().Reverse().ToArray();
        return Task.CompletedTask;
    }
}

Related Articles