Starting with NServiceBus version 8, the NServiceBus Host is no longer supported. Refer to the the host upgrade guide for further details and alternatives.
For endpoints using NServiceBus version 7 and above, it is no longer recommended to use the NServiceBus Host. Use alternative approaches such as Generic Host, NServiceBus Windows Service or NServiceBus Docker Container instead.
Code walk-through
This sample shows how to host an 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 at start and stop time
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 when the endpoint starts and stops. 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;
}
}