Endpoint configuration choices

Component: NServiceBus
NuGet Package NServiceBus (5.x)

Code walk-through

This sample walks through the most common choices required when creating a first endpoint. It will also show the configuration APIs needed to implement those choices.


This sample uses a dual runnable console and Windows Service for hosting. Details on how to install an endpoint as a Windows Service can be seen in Windows Service Installation. See also Hosting options.

Configure recoverability

When a message fails processing it will be forwarded here.

class ConfigErrorQueue :
    public MessageForwardingInCaseOfFaultConfig GetConfiguration()
        return new MessageForwardingInCaseOfFaultConfig
            ErrorQueue = "error"

In Version 5 and below this approach uses the IProvideConfiguration approach to programmatically override the error queue.

Configure an audit queue

All messages received by an endpoint will be forwarded to the audit queue.

class ConfigAuditQueue :
    public AuditConfig GetConfiguration()
        return new AuditConfig
            QueueName = "audit",

Select and configure logging

In this sample Log4net is being used to route log events to the console.

var layout = new PatternLayout
    ConversionPattern = "%d %-5p %c - %m%n"
var appender = new ConsoleAppender
    Layout = layout,
    Threshold = Level.Info



Create the root configuration instance

The following code will create the configuration instance and define the endpoint name.

var busConfiguration = new BusConfiguration();

Select and configure dependency injection

This sample uses Autofac with a customized instance passed into NServiceBus.

var builder = new ContainerBuilder();
// configure custom services
// builder.RegisterInstance(new MyService());
var container = builder.Build();
    customizations: customizations =>

Select and configure serialization

This sample uses the XML serializer.


Select and configure a transport

This sample uses the MSMQ transport.


Select and configure persistence

This sample uses the in-memory persistence.

busConfiguration.UsePersistence<InMemoryPersistence, StorageType.Sagas>();
busConfiguration.UsePersistence<InMemoryPersistence, StorageType.Subscriptions>();
busConfiguration.UsePersistence<InMemoryPersistence, StorageType.Timeouts>();

Start the endpoint

Enable installers and start the endpoint.

bus = Bus.Create(busConfiguration).Start();

Shut down the endpoint

The bus implements IDisposable and should be shut down when the process is shut down.

Handling critical errors

Since this sample is configured to run as a windows service, the action defined when a critical error occurs is to shut down the process.

    onCriticalError: (errorMessage, exception) =>
        // Log the critical error
        log.Fatal($"CRITICAL: {errorMessage}", exception);

        // Kill the process on a critical error
        var output = $"NServiceBus critical error:\n{errorMessage}\nShutting down.";
        Environment.FailFast(output, exception);

Related Articles

  • Assembly scanning
    To enable automatic detection of various features NServiceBus scans assemblies for well known types.
  • Hosting
    Describes the various approaches to endpoint hosting.

Last modified