Endpoint configuration choices

Component: NServiceBus | Nuget: NServiceBus (Version: 5.x)
This sample uses the same approach as the NServiceBus as a Windows Service for a streamlined Windows Service debugging experience.

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. More details on this approach can be seen in Windows Service Hosting, there is also a more detailed sample of this approach. See also Hosting options.

Configure an Recoverability

When a message fails processing it will be forwarded here.

Edit
class ConfigErrorQueue :
    IProvideConfiguration<MessageForwardingInCaseOfFaultConfig>
{
    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.

Edit
class ConfigAuditQueue :
    IProvideConfiguration<AuditConfig>
{
    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.

Edit
var layout = new PatternLayout
{
    ConversionPattern = "%d %-5p %c - %m%n"
};
layout.ActivateOptions();
var appender = new ConsoleAppender
{
    Layout = layout,
    Threshold = Level.Info
};
appender.ActivateOptions();

BasicConfigurator.Configure(appender);

LogManager.Use<Log4NetFactory>();

Create the root configuration instance

And also define the endpoint name.

Edit
var busConfiguration = new BusConfiguration();
busConfiguration.EndpointName("Samples.FirstEndpoint");

Select and configure a Container

Autofac is being used with a customized container instance being passed into NServiceBus.

Edit
var builder = new ContainerBuilder();
// configure custom services
// builder.RegisterInstance(new MyService());
var container = builder.Build();
busConfiguration.UseContainer<AutofacBuilder>(
    customizations: customizations =>
    {
        customizations.ExistingLifetimeScope(container);
    });

Select and configure Serialization

This sample uses the JSON.

Edit
busConfiguration.UseSerialization<JsonSerializer>();

Select and configure a Transport

This sample uses the MSMQ Transport.

Edit
busConfiguration.UseTransport<MsmqTransport>();

Select and configure Persistence

This sample uses InMemory persistence.

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

Start the Endpoint

Enable installers and start the endpoint.

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

Shut down the Endpoint

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

Edit
bus?.Dispose();

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.

Edit
busConfiguration.DefineCriticalErrorAction(
    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);
    });

Samples

Related Articles

  • Assembly scanning
    To enable automatic detection of various features NServiceBus scans assemblies for well known types.

Last modified