Self hosting

Component: NServiceBus | Nuget: NServiceBus (Version: 5.x)

Code walk-through

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

Edit
var busConfiguration = new BusConfiguration();
busConfiguration.EndpointName("Samples.SelfHosting");
busConfiguration.UseSerialization<JsonSerializer>();
busConfiguration.EnableInstallers();
busConfiguration.UsePersistence<InMemoryPersistence>();

using (var bus = Bus.Create(busConfiguration).Start())
{
    Console.WriteLine("\r\nBus created and configured; press any key to stop program\r\n");
    var myMessage = new MyMessage();
    bus.SendLocal(myMessage);
    Console.ReadKey();
}
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