AI agents: use the documentation index at llms.txt to locate machine-readable pages. This section is indexed by https://docs.particular.net/nservicebus/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.

Interfaces as messages

Component:
NServiceBus
NuGet Package:
NServiceBus 10.x

Events can be created on the fly from interfaces without first defining an explicit class implementing the interfaces. This technique can be used to support multiple inheritance for polymorphic routing scenarios. In general, it is recommended to use dedicated, simple types as messages instead.

Sending interface messages

Interface messages can be sent using the following syntax:

await messageSession.Send<IMyMessage>(message =>
{
    message.SomeProperty = "Hello world";
});

Replies are supported via:

return context.Reply<IMyReply>(message =>
{
    message.SomeProperty = "Hello world";
});

Publishing interface messages

Interface messages can be published using the following syntax:

return context.Publish<IMyEvent>(message =>
{
    message.SomeProperty = "Hello world";
});

Creating interface messages with IMessageCreator

If an interface message is needed before calling Send or Publish, use IMessageCreator directly to create the message instance:

//IMessageCreator is available via dependency injection
async Task PublishEvent(IMessageCreator messageCreator)
{
    var eventMessage = messageCreator.CreateInstance<IMyEvent>(message =>
    {
        message.SomeProperty = "Hello world";
    });

    await messageSession.Publish(eventMessage);

    //or if on a message handler

    await context.Publish(eventMessage);
}

Related Articles