Self hosting

Component: NServiceBus
NuGet Package NServiceBus (7-pre)
This page targets a pre-release version and is subject to change prior to the final release.

Code walk-through

This sample shows how to host an in-process instance of NServiceBus.

var endpointConfiguration = new EndpointConfiguration("Samples.SelfHosting");
endpointConfiguration.UsePersistence<LearningPersistence>();
endpointConfiguration.UseTransport<LearningTransport>();

var endpointInstance = await Endpoint.Start(endpointConfiguration)
    .ConfigureAwait(false);
try
{
    Console.WriteLine("\r\nBus created and configured; press any key to stop program\r\n");
    var myMessage = new MyMessage();
    await endpointInstance.SendLocal(myMessage)
        .ConfigureAwait(false);
    Console.ReadKey();
}
finally
{
    await endpointInstance.Stop()
        .ConfigureAwait(false);
}
Although not shown in this sample, when self-hosting NServiceBus, the critical error action should always be overridden. If a critical error occurs, NServiceBus will stop the endpoint but will not shut down the process. By specifying a critical error action, the host application can elect to terminate in order to be respawned, or take action to notify system administrators of the failure.

Related Articles

  • Hosting
    Outlines the various approaches to endpoint hosting.

Last modified