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
- Stop
Serverwith Ctrl+C and leaveClientrunning. - Press Enter in the
Clientterminal a few times. The client can still send messages while the server is offline. - Open
.in the solution folder. Each queued message has alearningtransport/ Samples. FaultTolerance. Server .file. Its first line points to the matchingmetadata. txt .file inbody. txt ., which contains the JSON payload.bodies
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/.
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.. 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 . 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/:
//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/ and Server/, then restart Server to process new messages normally. Messages already in the error queue remain there.