Hosting in Azure Cloud Services

Project Hosting
NuGet Package NServiceBus.Hosting.Azure (6.x)
Target NServiceBus Version: 5.x
The NServiceBus Azure Host will be deprecated as of Version 9. Refer to the upgrade guide for further details.

The Azure Platform and NServiceBus make a perfect fit. On the one hand the Azure platform offers the scalable and flexible platform required, on the other hand NServiceBus makes development on this highly distributed environment a breeze.

If real scale is required (e.g. in tens, hundreds or even thousands of machines hosting each endpoint) then Cloud Services is the required deployment model.

Cloud Services - Worker Roles

Reference the assembly that contains the Azure role entry point integration. The recommended way of doing this is by adding a NuGet package reference to the NServiceBus.Hosting.Azure package to the project.

To integrate the NServiceBus generic host into the worker role entry point, create an instance of NServiceBusRoleEntrypoint and call it's Start and Stop methods in the appropriate RoleEntryPoint override.

public class WorkerRole :
    RoleEntryPoint
{
    NServiceBusRoleEntrypoint nsb = new NServiceBusRoleEntrypoint();

    public override bool OnStart()
    {
        nsb.Start();
        return base.OnStart();
    }

    public override void OnStop()
    {
        nsb.Stop();
        base.OnStop();
    }
}

Next to starting the role entry point, define the endpoint behavior. The role has been named AsA_Worker. Specify the transport to use, using the UseTransport<T>, as well the persistence using the UsePersistence<T> configuration methods.

public class EndpointConfig :
    IConfigureThisEndpoint,
    AsA_Worker
{
    public void Customize(BusConfiguration busConfiguration)
    {
        var transport = busConfiguration.UseTransport<AzureServiceBusTransport>();
        transport.ConnectionString("Endpoint=sb://[NAMESPACE].servicebus.windows.net/;SharedAccessKeyName=[KEYNAME];SharedAccessKey=[KEY]");
        busConfiguration.UsePersistence<AzureStoragePersistence>();
    }
}

This will integrate and configure the default infrastructure:

  • Configuration setting will be read from the app.config file, merged with the settings from the service configuration file.
  • Logs will be sent to the Trace infrastructure, which should have been configured with Azure diagnostic monitor trace listener by the Visual Studio tooling.

When self-hosting everything can be configured using code API and extension methods available in the NServiceBus Azure related packages. In such a case it's not required to reference the hosting package. To self-host an endpoint add the required configuration to the role entry point. For more information how to self-host in Cloud Services refer to the Cloud Services self-hosting sample.

Cloud Services - Web Roles

Next to worker roles, cloud services also has a role type called 'Web Roles'. These are worker roles which have IIS configured properly, this means that they run a worker role process (the entry point is in webrole.cs) and an IIS process on the same codebase.

Usually NServiceBus is configured as a client in the IIS process. This needs to be approached in the same way as any other website, by means of self-hosting. When self-hosting everything can be configured using the configuration API and the extension methods found in the NServiceBus Azure related packages. No reference to the hosting package is required in that case.

The configuration API is used with the following extension methods to achieve the same behavior as the AsA_worker role:

public class MvcApplication :
    HttpApplication
{
    protected void Application_Start()
    {
        var busConfiguration = new BusConfiguration();
        busConfiguration.AzureConfigurationSource();
        busConfiguration.UseTransport<AzureStorageQueueTransport>();
        busConfiguration.UsePersistence<AzureStoragePersistence>();
        var startableBus = Bus.Create(busConfiguration);
        var bus = startableBus.Start();
    }
}

A short explanation of each:

  • AzureConfigurationSource: overrides any settings known to the NServiceBus Azure configuration section within the app.config file with settings from the service configuration file.
  • Logs will be sent to the Trace infrastructure, which should have been configured with Azure diagnostic monitor trace listener by the Visual Studio tooling.
  • UseTransport<AzureStorageQueueTransport>: Sets Azure storage queues as the transport.
  • UsePersistence<AzureStoragePersistence>: Configures Azure storage for persistence.

When Endpoint Instance Starts and Stops

public class Bootstrapper :
    IWantToRunWhenBusStartsAndStops
{

    public void Start()
    {
        // Do startup actions here.
    }

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

Samples

Related Articles


Last modified