The NServiceBus.Host

Component: NServiceBus Host
NuGet Package NServiceBus.Host (8-pre)
Target NServiceBus Version: 7.x
This page targets a pre-release version and is subject to change prior to the final release.
The NServiceBus host is being deprecated as of Version 9. Refer to the upgrade guide for further details.

Code walk-through

This sample shows how to host a NServiceBus endpoint using the NServiceBus.Host.

[EndpointName("Samples.NServiceBus.Host")]
public class EndpointConfig :
    IConfigureThisEndpoint
{
    public void Customize(EndpointConfiguration endpointConfiguration)
    {
        endpointConfiguration.EnableInstallers();
        endpointConfiguration.UsePersistence<LearningPersistence>();
        endpointConfiguration.UseTransport<LearningTransport>();
    }
}

Running code when at start and stop

Since the configuration is done via an implementation of IConfigureThisEndpoint and the host controls the startup process there is no regular way to run code at startup and stop of the endpoint. To enable this scenario there is an interface that is assembly scanned and executed at these times.

public class MessageSender :
    IWantToRunWhenEndpointStartsAndStops
{
    public Task Start(IMessageSession session)
    {
        var myMessage = new MyMessage();
        return session.SendLocal(myMessage);
    }

    public Task Stop(IMessageSession session)
    {
        return Task.CompletedTask;
    }
}

Related Articles

  • Interface life-cycles
    The life-cycles of the various NServiceBus configuration interfaces.
  • NServiceBus Host
    Avoid writing repeat configuration code, host the endpoints in a Windows Service, and change technologies without code.

Last modified