Getting Started
Architecture
NServiceBus
Transports
Persistence
ServiceInsight
ServicePulse
ServiceControl
Monitoring
Samples

NServiceBus Host Upgrade Version 6 to 7

Component: NServiceBus Host

This is part of the NServiceBus Upgrade Guide from Version 5 to 6, which also includes the following individual upgrade guides for specific components:

Feature Details
Transports
Persistence
Hosting
Other

IConfigureThisEndpoint changes

IConfigureThisEndpoint.Customize is passed an instance of EndpointConfiguration instead of BusConfiguration.

// For NServiceBus Host version 7.x
class CustomizingHostUpgrade :
    IConfigureThisEndpoint
{
    public void Customize(EndpointConfiguration endpointConfiguration)
    {
        // perform some custom configuration
        endpointConfiguration.UsePersistence<InMemoryPersistence>();
    }
}

// For NServiceBus Host version 6.x
class CustomizingHostUpgrade :
    IConfigureThisEndpoint
{
    public void Customize(BusConfiguration busConfiguration)
    {
        // perform some custom configuration
        busConfiguration.UsePersistence<InMemoryPersistence>();
    }
}

IConfigureLogging and IConfigureLoggingForProfile changes

These interfaces will be removed in version 8 of NServiceBus.Host. The logging can still be configured in the constructor of the class that implements IConfigureThisEndpoint.

// For NServiceBus Host version 8.x
class CustomLogging :
    IConfigureThisEndpoint
{
    public void Customize(EndpointConfiguration configuration)
    {
        LogManager.Use<DefaultFactory>();
    }
}

// For NServiceBus Host version 7.x
class CustomLogging :
    IConfigureThisEndpoint
{
    public void Customize(EndpointConfiguration configuration)
    {
        LogManager.Use<DefaultFactory>();
    }
}

// For NServiceBus Host version 6.x
class CustomLogging : IConfigureThisEndpoint
{
    public void Customize(BusConfiguration configuration)
    {
        LogManager.Use<DefaultFactory>();
    }
}

// For NServiceBus Host version 5.x
class CustomLogging : IConfigureThisEndpoint
{
    public void Customize(BusConfiguration configuration)
    {
        LogManager.Use<DefaultFactory>();
    }
}

The way the runtime profile is detected will need to be re-created but a simple approach could be like this:

class RuntimeProfile :
    IConfigureThisEndpoint
{
    public void Customize(BusConfiguration busConfiguration)
    {
        var profile = Environment.GetCommandLineArgs();

        if (profile.Contains("Production"))
        {
            // configure the production profile
        }
    }
}

IWantToRunWhenEndpointStartsAndStops

An interface called IWantToRunWhenEndpointStartsAndStops has been added. This interface replaces the IWantToRunWhenBusStartsAndStops in NServiceBus core.

Interface in version 5 of NServiceBus

public class Bootstrapper :
    IWantToRunWhenBusStartsAndStops
{
    public void Start()
    {
        // Do startup actions here.
    }

    public void Stop()
    {
        // Do cleanup actions here.
    }
}

Interface in version 7 of NServiceBus.Host

// Use this in NServiceBus.Host or NServiceBus.Host.AzureCloudService
namespace Host_7.UpgradeGuides.Core5to6
{
    public class Bootstrapper :
        IWantToRunWhenEndpointStartsAndStops
    {
        public Task Start(IMessageSession session)
        {
            // Do startup actions here.
            // Either mark Start method as async or do the following
            return Task.CompletedTask;
        }

        public Task Stop(IMessageSession session)
        {
            // Do cleanup actions here.
            // Either mark Stop method as async or do the following
            return Task.CompletedTask;
        }
    }
}

The IMessageSession parameter provides all the necessary methods to send messages as part of the endpoint start up.

Visual Studio PowerShell helpers removed

In NServiceBus versions 3 through 5, there is a NuGet package that provides helpers to generate XML configuration sections using the PowerShell console in Visual Studio.

For example, running the Add-NServiceBusAuditConfig ProjectName command would add the following section to the app.config file:

<configuration>
  <configSections>
    <section name="AuditConfig"
             type="NServiceBus.Config.AuditConfig, NServiceBus.Core" />
  </configSections>
  <AuditConfig QueueName="audit" />
</configuration>

These helpers have been removed in version 6. The configuration helpers encouraged creating a more complex XML configuration than was necessary, making it difficult to manage in the long run.

The recommended configuration approach is "code-first", which is more flexible and less error-prone than using the PowerShell helpers. The configuration can be read from any location at runtime, including the app.config. For more information about the API, refer to the configuration documentation dedicated to the particular functionality. For example, see how recoverability and audit queues can be configured using code API.

WCF integration

WCF integration using WcfService has been moved from the host to a separate NuGet package NServiceBus.Wcf. That package must be used in order to use the WCF integration functionality when targeting NServiceBus version 6 and above. The NServiceBus.Wcf NuGet package has no dependency on the NServiceBus.Host NuGet package and can also be used in self-hosting scenarios.

The WCF integration has been augmented with additional functionality such as the ability to reply with messages to the client, cancel requests and reroute to other endpoints. More information can be found in WCF.

Ambiguous type compilation error

By default referenced assemblies are imported into the global namespace. This might lead to ambiguous type reference problems when implementing WcfService<TRequest, TResponse> or IWcfService<TRequest, TResponse>. To resolve the ambiguous type reference:

  • In Solution Explorer, right-click under References on the NServiceBus.Wcf reference and switch to the properties pane, or use Alt+Enter
  • Under the alias property set an alias for the assembly, for example wcf
  • In the declaration of the WCF services use:
extern alias wcf;
using wcf::NServiceBus;

Related Articles