Running in development mode
- Start Azure Storage Emulator
- Run the solution
- Inspect Azure Storage Emulator
MultiHostedEndpointsOutput
table for content similar to:
PartitionKey | RowKey | Timestamp | Message |
---|---|---|---|
Sender | 2015-05-25 14:58:33 | 2015-05-25 16:58:33 | Pinging Receiver |
Receiver | 2015-05-25 14:58:34 | 2015-05-25 16:58:34 | Got Ping and will reply with Pong |
Sender | 2015-05-25 14:58:35 | 2015-05-25 16:58:35 | Got Pong from Receiver |
Results sorted by Timestamp
Deploying endpoints
- Open PowerShell console at the
shared-host\
location. This location should containCloudServicesHost_{version} PackageAndDeploy.
.ps1 - Execute
PackageAndDeploy.
PowerShell script to package and deploy multi-hosted endpoints to local emulator storageps1
Running multi-host in emulated Cloud Service
- Set
HostCloudService
to be the start-up project by right clicking the project name in Visual Studio Solution Explorer, and selectingSet as StartUp Project
option - Run the solution
- Inspect Azure Storage Emulator Tables for
MultiHostedEndpointsOutput
table and its content
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.
process and killing all child processes named NServiceBus.
. 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 theReceiver
endpoint processing. - Receiver - An NServiceBus endpoint that receives
Ping
command and responds back to originator withPong
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();
routing.RouteToEndpoint(
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 :
IWantToRunWhenEndpointStartsAndStops
{
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 :
IHandleMessages<Pong>
{
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 :
IHandleMessages<Ping>
{
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 :
ICommand
{
}
public class Pong :
IMessage
{
}
HostWorker project
HostWorker project is the multi-host project. To enable multi-hosting, endpoint is configured as a multi-host
HostCloudService project
HostCloudService project defines multi-host parameters for all environment (Local
and Cloud
in this sample)
Values provided to execute sample against local Azure Storage emulator