Endpoint API changes in NServiceBus Version 6

Component: NServiceBus

Endpoint name is mandatory

In NServiceBus versions 6 and above, endpoint name is mandatory.

6.x NServiceBus
var endpointConfiguration = new EndpointConfiguration("MyEndpointName");
5.x NServiceBus
var busConfiguration = new BusConfiguration();
busConfiguration.EndpointName("MyEndpointName");

The endpoint name is used as a logical identifier when sending or receiving messages. It is also used for determining the name of the input queue the endpoint will be bound to. See Derived endpoint name for the algorithm used in NServiceBus versions 5 and below to select endpoint name if backwards compatibility is a concern.

Interface changes

The IWantToRunWhenBusStartsAndStops interface is now obsolete.

Self-hosting

When self-hosting, call any startup code after Endpoint.Start and cleanup code after Endpoint.Stop.

6.x NServiceBus
var endpointConfiguration = new EndpointConfiguration("EndpointName");

// Custom code before start
var endpointInstance = await Endpoint.Start(endpointConfiguration)
    .ConfigureAwait(false);
// Custom code after start

// Block the process

// Custom code before stop
await endpointInstance.Stop()
    .ConfigureAwait(false);
// Custom code after stop
5.x NServiceBus
var busConfiguration = new BusConfiguration();

// Custom code before start
var startableBus = Bus.Create(busConfiguration);
using (var bus = startableBus.Start())
{
    // Custom code after start

    // Block the process

    // Custom code before stop
}
// Custom code after stop

While the Dispose Pattern can no longer be used (since IEndpointInstance does not implement IDisposable) this is not a common use case since in most hosting scenarios, the startup code is not in the same method as the shutdown code. For example

If the extensibility provided by IWantToRunWhenBusStartsAndStops is still required, it can be achieved via other means, such as using MEF or reflection to customize NServiceBus.

Using NServiceBus.Host

See the upgrade guide for more details on using the new interface provided by the host.

Using AzureCloudService Host

See the upgrade guide for more details on using the new interface provided by the host.

Endpoint name helper

An algorithm is used in NServiceBus version 5 to determine the name of the endpoint if none is provided. If the same behavior is needed in versions 6 and above, this helper class can be used.

6.x NServiceBus
public static class EndpointNameHelper
{
    public static string GetDefaultEndpointName()
    {
        var entryType = GetEntryType();

        if (entryType != null)
        {
            var endpointName = entryType.Namespace ?? entryType.Assembly.GetName().Name;
            if (endpointName != null)
            {
                return endpointName;
            }
        }

        throw new Exception("No endpoint name could be derived");
    }

    static Type GetEntryType()
    {
        var stackTraceToExamine = new StackTrace();
        var entryAssembly = Assembly.GetEntryAssembly();
        if (entryAssembly?.EntryPoint != null)
        {
            return entryAssembly.EntryPoint.ReflectedType;
        }

        StackFrame targetFrame = null;

        var stackFrames = new StackTrace().GetFrames();
        if (stackFrames != null)
        {
            targetFrame =
                stackFrames.FirstOrDefault(
                    f => typeof(HttpApplication).IsAssignableFrom(f.GetMethod().DeclaringType));
        }

        if (targetFrame != null)
        {
            return targetFrame.GetMethod().ReflectedType;
        }

        stackFrames = stackTraceToExamine.GetFrames();
        if (stackFrames != null)
        {
            targetFrame =
                stackFrames.FirstOrDefault(
                    f =>
                    {
                        var declaringType = f.GetMethod().DeclaringType;
                        return declaringType != typeof(EndpointConfiguration);
                    });
        }

        if (targetFrame == null)
        {
            targetFrame = stackFrames.FirstOrDefault(
                f => f.GetMethod().DeclaringType.Assembly != typeof(EndpointConfiguration).Assembly);
        }

        if (targetFrame != null)
        {
            return targetFrame.GetMethod().ReflectedType;
        }
        throw new Exception("Could not derive EndpointName");
    }

}

Last modified