Endpoint API changes in Version 6

Component: NServiceBus

Endpoint Name is mandatory

In 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 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 or any 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 actually 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, for example, 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

This algorithm is used in Version 5 of NServiceBus to determine Endpoint Name if none is provided. So if the same behavior is is desired 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