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.

Fault Tolerance

Component:
NServiceBus
NuGet Package:
NServiceBus 9.x

This sample uses the Learning Transport. From the downloaded solution folder, run dotnet run --project Server and dotnet run --project Client in separate terminals. Press any key in the Server terminal to start the endpoint.

When both endpoints are running, press Enter in the Client terminal to send a message. The client and server log the same ID in different formats.

Client output:

Sent a message with id: 1a5ed0303772492cabac0764a70ca29b

Server output:

Message received. Id: 1a5ed030-3772-492c-abac-0764a70ca29b

Queue up multiple messages

  1. Stop Server with Ctrl+C and leave Client running.
  2. Press Enter in the Client terminal a few times. The client can still send messages while the server is offline.
  3. Open .learningtransport/Samples.FaultTolerance.Server in the solution folder. Each queued message has a .metadata.txt file. Its first line points to the matching .body.txt file in .bodies, which contains the JSON payload.

Consume those messages

Restart Server and press any key to start the endpoint. The server logs the IDs of the queued messages as it processes them, and their files disappear from the queue directory.

Fault tolerance

Make the handler fail

Stop Server and open Server/MyHandler.cs.

public class MyHandler(ILogger<MyHandler> logger):
    IHandleMessages<MyMessage>
{
    public Task Handle(MyMessage message, IMessageHandlerContext context)
    {
        logger.LogInformation("Message received. Id: {Id}", message.Id);
        // throw new System.Exception("Uh oh - something went wrong....");
        return Task.CompletedTask;
    }
}

Uncomment the throw line and remove return Task.CompletedTask;. Restart Server and send one message from Client.

The handler logs the same ID each time it runs. NServiceBus makes five immediate retries on the initial delivery and again after each delayed retry. By default, the three delayed retries wait 10, 20, and 30 seconds. If all attempts fail, the message moves to .learningtransport/error after about a minute. The server keeps running.

These lines show the first delayed retry and the move to the error queue. The logs use the transport message ID, not the ID from the message body that the handler logs.

Delayed Retry will reschedule message 'bcf676e8-bfcd-42d1-921f-b512007afedd' after a delay of 00:00:10 because of an exception:
Moving message 'bcf676e8-bfcd-42d1-921f-b512007afedd' to the error queue 'error' because processing failed due to an exception:

To disable delayed retries, stop Server and uncomment this block in Server/Program.cs:

//var recoverability = endpointConfiguration.Recoverability();
//recoverability.Delayed(settings =>
//{
//    settings.NumberOfRetries(0);
//});

Leave the exception in place, restart Server, and send another message. The server moves it to the error queue after five immediate retries without scheduling any delayed retries.

Restore Server/MyHandler.cs and Server/Program.cs, then restart Server to process new messages normally. Messages already in the error queue remain there.

List of Samples

  • Automatic Retries
    Shows immediate and delayed retries when a handler throws an exception.

Related Articles

  • Recoverability
    Explains how exceptions are handled, and actions retried, during message processing.