Self-Hosting in Azure Cloud Services

Component: NServiceBus
NuGet Package NServiceBus (7.x)

Running in development mode

  1. Start Azure Storage Emulator
  2. Run the solution
  3. Inspect Azure Storage Emulator Tables for SelfHostedEndpointsOutput table and its content for something like the following:
MyMessageHandler2017-09-19 10:19:082017-09-19T08:19:27.691ZGot MyMessage.

Results are sorted by Timestamp

Deploying endpoints

  1. Open a PowerShell console at the self-host\CloudServicesHost_{version} location. This location should contain a file called PackageAndDeploy.ps1.
  2. Execute the PackageAndDeploy.ps1 PowerShell script to package and deploy multi-hosted endpoints to local emulator storage.

Running a self-host in emulated Cloud Service

  1. Set HostCloudService to be the start-up project by right-clicking the project name in Visual Studio Solution Explorer, and selecting Set as StartUp Project option
  2. Run the solution
  3. Inspect Azure Storage Emulator tables for SelfHostedEndpointsOutput table and its content
To inspect multi-host role emulated file system navigate to C:\Users\%USERNAME%\AppData\Local\dftmp\Resources

Azure Compute Emulator leaves any processes spawned at run time in memory. These can be killed by locating the WaWorkerHost.exe process and killing all child processes named NServiceBus.Hosting.Azure.HostProcess.exe. The number of those processes will be the same as the number of endpoints (i.e. one) x the number of times Cloud Service was executed.

Cloud Service emulator can also be stopped with the Compute Emulator UI. The Compute Emulator UI can be accessed via the try icon on the taskbar. Within Compute Emulator UI, under Service Deployments tree select a deployment, right-click, and select the Remove option. This will cleanly stop Cloud Service without leaving any processes in memory.

Code walk-through

This sample contains five projects:

  • HostWorker - Self-host endpoint deployed as worker role.
  • HostCloudService - Azure Cloud Service project to define and execute cloud service.

HostWorker project

HostWorker project uses the self-hosting capability to start an endpoint inside a worker role.

The snippet below illustrates how the OnStart method of RoleEntryPoint calls in a blocking way into an asynchronous start method.

public override bool OnStart()
    return base.OnStart();

static async Task StartEndpoint()

A critical error action needs to be defined to restart the host when a critical error is raised.

    onCriticalError: context =>
        if (Environment.UserInteractive)
            // so that user can see on their screen the problem

        var message = $"Critical error encountered:\n{context.Error}\nNServiceBus is shutting down.";
        Environment.FailFast(message, context.Exception);
        return Task.CompletedTask;

To uniquely identify the host a custom name and display name must be provided.

if (SafeRoleEnvironment.IsAvailable)
    var host = SafeRoleEnvironment.CurrentRoleName;
    var instance = SafeRoleEnvironment.CurrentRoleInstanceId;
    var displayName = $"{host}_{instance}";
        .UsingNames(instance, host)

The connection string can be loaded using the RoleEnvironment as shown below.

var connectionString = RoleEnvironment.GetConfigurationSettingValue("HostWorker.ConnectionString");
var persistence = configuration.UsePersistence<AzureStoragePersistence>();
var transport = configuration.UseTransport<AzureStorageQueueTransport>();

HostCloudService project

The HostCloudService project defines self-host parameters for all environments (Local and Cloud in this sample)

<WorkerRole name="HostWorker" vmsize="Small">
    <Setting name="HostWorker.ConnectionString" />
    <Setting name="Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString" />
    <LocalStorage name="endpoints" cleanOnRoleRecycle="true" sizeInMB="100" />

Values provided to execute the sample against local Azure Storage emulator

<Role name="HostWorker">
  <Instances count="1" />
    <Setting name="HostWorker.ConnectionString"
             value="UseDevelopmentStorage=true" />
    <Setting name="Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString"
             value="UseDevelopmentStorage=true" />

Related Articles

  • Shared Hosting in Azure
    Using Azure cloud services to host multiple NServiceBus endpoints on a shared pool of machines.

Last modified