Getting Started
Architecture
NServiceBus
Transports
Persistence
ServiceInsight
ServicePulse
ServiceControl
Monitoring

Endpoint configuration choices

This sample is obsolete. Particular recommends using the .NET Generic Host sample.

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.

Hosting

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.

endpointConfiguration.SendFailedMessagesTo("error");

Configure an audit queue

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

endpointConfiguration.AuditProcessedMessagesTo("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"
};
layout.ActivateOptions();
var appender = new ConsoleAppender
{
    Layout = layout,
    Threshold = Level.Info
};
appender.ActivateOptions();

var executingAssembly = Assembly.GetExecutingAssembly();
var repository = log4net.LogManager.GetRepository(executingAssembly);
BasicConfigurator.Configure(repository, appender);

LogManager.Use<Log4NetFactory>();

Create the root configuration instance

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

In Versions 6 and above, the endpoint name is mandatory.
var endpointConfiguration = new EndpointConfiguration("Samples.FirstEndpoint");

Select and configure dependency injection

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

endpointConfiguration.UseContainer(new AutofacServiceProviderFactory(builder =>
{
    // configure custom services
    // builder.RegisterInstance(new MyService());
}));

Select and configure serialization

This sample uses the XML serializer.

endpointConfiguration.UseSerialization<XmlSerializer>();

Select and configure a transport

This sample uses the learning transport.

endpointConfiguration.UseTransport<LearningTransport>();

Select and configure persistence

This sample uses the learning persistence.

endpointConfiguration.UsePersistence<LearningPersistence>();

Start the endpoint

Enable installers and start the endpoint.

endpointConfiguration.EnableInstallers();
endpointInstance = await Endpoint.Start(endpointConfiguration);

Shut down the endpoint

The endpoint should be stopped when the process is shut down.

endpointInstance?.Stop().GetAwaiter().GetResult();

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.

endpointConfiguration.DefineCriticalErrorAction(
    onCriticalError: async context =>
    {
        // Log the critical error
        log.Fatal($"CRITICAL: {context.Error}", context.Exception);

        await context.Stop();

        // Kill the process on a critical error
        var output = $"NServiceBus critical error:\n{context.Error}\nShutting down.";
        Environment.FailFast(output, context.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.