Raw messaging using NServiceBus

NuGet Package NServiceBus.Raw (2.x) | License
This is a community-maintained project. License and support are independent of Particular Software.
Target NServiceBus Version: 6.x
Standard support for version 6.x of NServiceBus has expired. For more information see our Support Policy.

NServiceBus.Raw allows sending and receiving raw messages using the NServiceBus transport infrastructure. It is flexible in terms of message manipulation, therefore it is a good fit for integrations with 3rd party systems, building gateways and bridges.


Configuration of raw endpoints is similar to the standard NServiceBus endpoint configuration:

var senderConfig = RawEndpointConfiguration.Create(
    endpointName: "EndpointName",
    onMessage: OnMessage,
    poisonMessageQueue: "error");

var sender = await RawEndpoint.Start(senderConfig)


The following code sends a message to another endpoint:

var body = Serialize();
var headers = new Dictionary<string, string>
    ["SomeHeader"] = "SomeValue"
var request = new OutgoingMessage(
    messageId: Guid.NewGuid().ToString(),
    headers: headers,
    body: body);

var operation = new TransportOperation(
    new UnicastAddressTag("Receiver"));

await sender.Dispatch(
        outgoingMessages: new TransportOperations(operation),
        transaction: new TransportTransaction(),
        context: new ContextBag())


The following code implements the callback invoked when a message arrives at a raw endpoint:

static Task OnMessage(MessageContext context, IDispatchMessages dispatcher)
    var message = Deserialize(context.Body);

    // Can use dispatcher to send messages from here

    return Task.CompletedTask;

Notice the method gets a dispatcher object which can be used to send messages. The transport transaction object can be passed from the receiving context to the dispatcher, in order to ensure transactions span both send and receive. Make sure that the underlying transport infrastructure supports the SendsAtomicWithReceive mode when using this option.

Last modified