Shared Hosting in Azure Cloud Services

Component: Azure Cloud Service Host
NuGet Package NServiceBus.Hosting.Azure (8.x)
Target NServiceBus Version: 7.x

Running in development mode

  1. Start Azure Storage Emulator
  2. Run the solution
  3. Inspect Azure Storage Emulator MultiHostedEndpointsOutput table for content similar to:
Sender2015-05-25 14:58:332015-05-25 16:58:33Pinging Receiver
Receiver2015-05-25 14:58:342015-05-25 16:58:34Got Ping and will reply with Pong
Sender2015-05-25 14:58:352015-05-25 16:58:35Got Pong from Receiver

Results sorted by Timestamp

Deploying endpoints

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

Running multi-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 MultiHostedEndpointsOutput 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. Kill those once done with emulated Cloud Service execution by locating WaWorkerHost.exe process and killing all child processes named NServiceBus.Hosting.Azure.HostProcess.exe. Number of those processes will equal the number of endpoints (which in this case equals 2) multiplied by the number of times Cloud Service was executed.

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

Code walk-through

This sample contains five projects:

  • Shared - A class library containing shared code including the message definitions.
  • Sender - An NServiceBus endpoint responsible for sending Ping command to the Receiver endpoint processing.
  • Receiver - An NServiceBus endpoint that receives Ping command and responds back to originator with Pong message.
  • HostWorker - Multi-host endpoint deployed as worker role.
  • HostCloudService - Azure Cloud Service project to define and execute cloud service.

Sender project

Sender project defines message mapping instructing NServiceBus to send Ping commands to the Receiver endpoint.

var transport = endpointConfiguration.UseTransport<AzureStorageQueueTransport>();
var routing = transport.Routing();
    messageType: typeof(Ping),
    destination: "Receiver");

When the endpoint is started, a Ping command is sent and a log entry is written to Azure Storage Tables (see the Shared project for details on the log generation).

public class SendPing :
    public Task Start(IMessageSession session)
        VerificationLogger.Write("Sender", "Pinging Receiver");
        return session.Send(new Ping());

    public Task Stop(IMessageSession session)
        return Task.CompletedTask;

Sender defines a handler for messages of type Pong and writes to the log when a message of this type arrives.

public class PongHandler :
    public Task Handle(Pong message, IMessageHandlerContext context)
        VerificationLogger.Write("Sender", "Got Pong from Receiver");
        return Task.CompletedTask;

Receiver project

Receiver project has a handler for Ping commands and it writes to the log when such a message arrives. It also replies to the originator with the Pong message.

public class PingHandler :
    public Task Handle(Ping message, IMessageHandlerContext context)
        VerificationLogger.Write("Receiver", "Got Ping and will reply with Pong");
        return context.Reply(new Pong());

Shared project

Shared project defines all the messages used in the sample

public class Ping :
public class Pong :

HostWorker project

HostWorker project is the multi-host project. To enable multi-hosting, endpoint is configured as a multi-host

public class EndpointConfig :
    public void Customize(EndpointConfiguration endpointConfiguration)

    public HostingSettings Configure()
        var connectionString = RoleEnvironment.GetConfigurationSettingValue("HostWorker.ConnectionString");
        var autoUpdate = bool.Parse(RoleEnvironment.GetConfigurationSettingValue("HostWorker.AutoUpdate"));
        var updateInterval = int.Parse(RoleEnvironment.GetConfigurationSettingValue("HostWorker.UpdateInterval"));
        var container = RoleEnvironment.GetConfigurationSettingValue("HostWorker.Container");

        return new HostingSettings(connectionString)
            AutoUpdate = autoUpdate,
            UpdateInterval = updateInterval,
            Container = container
Multi-host project is used only as a host for other endpoints and contains no NServiceBus related logic

HostCloudService project

HostCloudService project defines multi-host parameters for all environment (Local and Cloud in this sample)

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

Values provided to execute sample against local Azure Storage emulator

<Role name="HostWorker">
  <Instances count="1" />
    <Setting name="HostWorker.ConnectionString"
             value="UseDevelopmentStorage=true" />
    <Setting name="HostWorker.AutoUpdate"
             value="true" />
    <Setting name="HostWorker.UpdateInterval"
             value="5000" />
    <Setting name="HostWorker.Container"
             value="endpoints" />
    <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