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.

Self-Hosting in Azure WebJobs

Component:
NServiceBus
NuGet Package:
NServiceBus 10.x

This is an example of how an NServiceBus endpoint can be hosted using Azure WebJobs. This sample is compatible with Azure WebJobs SDK 3.0.

Running in development mode

  1. Start the Azurite Storage Emulator.
  2. Run the solution.

Code walk-through

This sample contains one project:

  • Receiver: A self-hosted endpoint running in a continuous WebJob.

Receiver

The receiver uses the self-hosting capability to start an endpoint inside a WebJob.

The UseNServiceBus method of NServiceBus.Extensions.Hosting is used to configure and start the endpoint:

hostBuilder.ConfigureServices((hostBuilderContext, services) =>
{
    var endpointConfiguration = new EndpointConfiguration("receiver");
    endpointConfiguration.DefineCriticalErrorAction(OnCriticalError);
    endpointConfiguration.UsePersistence<NonDurablePersistence>();
    endpointConfiguration.UseSerialization<SystemJsonSerializer>();
    endpointConfiguration.EnableInstallers();

    var transportConnectionString = hostBuilderContext.Configuration.GetConnectionString("TransportConnectionString");
    endpointConfiguration.UseTransport(new AzureStorageQueueTransport(transportConnectionString));

    services.AddNServiceBusEndpoint(endpointConfiguration);
});

A critical error action must be defined to restart the host when a critical error occurs:

static async Task OnCriticalError(ICriticalErrorContext context, CancellationToken cancellationToken)
{
    var fatalMessage =
        $"The following critical error was encountered:{Environment.NewLine}{context.Error}{Environment.NewLine}Process is shutting down. StackTrace: {Environment.NewLine}{context.Exception.StackTrace}";

    try
    {
        await context.Stop(cancellationToken);
    }
    finally
    {
        Environment.FailFast(fatalMessage, context.Exception);
    }
}

When the WebJob host stops, the endpoint is automatically stopped with it by the hosting extension.