Shared Hosting in Azure Cloud Services

Component: Azure Cloud Service Host | Nuget: NServiceBus.Hosting.Azure (Version: 6.x)
Target NServiceBus Version: 5.x

Running in development mode

  1. Start Azure Storage Emulator
  2. Run the solution
  3. Inspect Azure Storage Emulator Tables for MultiHostedEndpointsOutput table and its content for something like the following:
PartitionKeyRowKeyTimestampMessage
Sender2015-05-25 14:58:335/25/2015 8:58:33 PMPinging Receiver
Receiver2015-05-25 14:58:345/25/2015 8:58:34 PMGot Ping and will reply with Pong
Sender2015-05-25 14:58:355/25/2015 8:58:35 PMGot 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 be as number of endpoints (2) X 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 cleanly 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 designated for 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 to instruct NServiceBus to send Ping commands to Receiver endpoint.

Edit
public class ConfigureMessageEndpointMappings :
    IProvideConfiguration<UnicastBusConfig>
{
    public UnicastBusConfig GetConfiguration()
    {
        return new UnicastBusConfig
        {
            MessageEndpointMappings = new MessageEndpointMappingCollection
            {
                new MessageEndpointMapping
                {
                    Endpoint = "Receiver",
                    Messages = "Ping, Shared"
                }
            }
        };
    }
}

When Bus is started, ping command is sent and a custom verification log is written to Azure Storage Tables (see Shared project for details about verification log table).

Edit
public class SendPing :
    IWantToRunWhenBusStartsAndStops
{
    IBus bus;

    public SendPing(IBus bus)
    {
        this.bus = bus;
    }

    public void Start()
    {
        VerificationLogger.Write("Sender", "Pinging Receiver");
        bus.Send(new Ping());
    }

    public void Stop()
    {
    }
}

Sender also defines a handler for messages of type Pong and writes into verification log when such arrives.

Edit
public class PongHandler :
    IHandleMessages<Pong>
{
    public void Handle(Pong message)
    {
        VerificationLogger.Write("Sender", "Got Pong from Receiver");
    }
}

Receiver project

Receiver project has a handler for Ping commands and it writes into verification log when such arrives and replies back to originator with Pong message.

Edit
public class PingHandler :
    IHandleMessages<Ping>
{
    IBus bus;

    public PingHandler(IBus bus)
    {
        this.bus = bus;
    }

    public void Handle(Ping message)
    {
        VerificationLogger.Write("Receiver", "Got Ping and will reply with Pong");
        bus.Reply(new Pong());
    }
}

Shared project

Shared project defines all the messages used in the sample

Edit
public class Ping :
    ICommand
{
}
Edit
public class Pong :
    IMessage
{
}

HostWorker project

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

Edit
public class EndpointConfig :
    IConfigureThisEndpoint,
    AsA_Host
{
    public void Customize(BusConfiguration busConfiguration)
    {
    }
}
Multi-host project is used solely as a host for other endpoints

HostCloudService project

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

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

Values provided to execute sample against local Azure Storage emulator

Edit
<Role name="HostWorker">
  <Instances count="1" />
  <ConfigurationSettings>
    <Setting name="DynamicHostControllerConfig.ConnectionString"
             value="UseDevelopmentStorage=true" />
    <Setting name="DynamicHostControllerConfig.AutoUpdate"
             value="true" />
    <Setting name="DynamicHostControllerConfig.UpdateInterval"
             value="5000" />
    <Setting name="DynamicHostControllerConfig.Container"
             value="endpoints" />
    <Setting name="Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString"
             value="UseDevelopmentStorage=true" />
  </ConfigurationSettings>
</Role>

Related Articles

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

Last modified