Running in development mode
- Start Azure Storage Emulator
- Run the solution
- Inspect Azure Storage Emulator Tables for
SelfHostedEndpointsOutput
table and its content for something like the following:
PartitionKey | RowKey | Timestamp | Message |
---|---|---|---|
MyMessageHandler | 2017-09-19 10:19:08 | 2017-09-19T08:19:27.691Z | Got MyMessage. |
Results are sorted by Timestamp
Deploying endpoints
- Open a PowerShell console at the
self-host\
location. This location should contain a file calledCloudServicesHost_{version} PackageAndDeploy.
.ps1 - Execute the
PackageAndDeploy.
PowerShell script to package and deploy multi-hosted endpoints to local emulator storage.ps1
Running a self-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
SelfHostedEndpointsOutput
table and its content
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.
process and killing all child processes named NServiceBus.
. 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()
{
StartEndpoint().GetAwaiter().GetResult();
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.
configuration.DefineCriticalErrorAction(
onCriticalError: context =>
{
if (Environment.UserInteractive)
{
// so that user can see on their screen the problem
Thread.Sleep(10000);
}
var message = $"Critical error encountered:\n{context.Error}\nNServiceBus is shutting down.";
LogManager.GetLogger(typeof(WorkerRole)).Fatal(message);
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}";
configuration
.UniquelyIdentifyRunningInstance()
.UsingNames(instance, host)
.UsingCustomDisplayName(displayName);
}
The connection string can be loaded using the RoleEnvironment
as shown below.
var connectionString = RoleEnvironment.GetConfigurationSettingValue("HostWorker.ConnectionString");
var persistence = configuration.UsePersistence<AzureStoragePersistence>();
persistence.ConnectionString(connectionString);
var transport = configuration.UseTransport<AzureStorageQueueTransport>();
transport.ConnectionString(connectionString);
HostCloudService project
The HostCloudService project defines self-host parameters for all environments (Local
and Cloud
in this sample)
<WorkerRole name="HostWorker" vmsize="Small">
<ConfigurationSettings>
<Setting name="HostWorker.ConnectionString" />
<Setting name="Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString" />
</ConfigurationSettings>
<LocalResources>
<LocalStorage name="endpoints" cleanOnRoleRecycle="true" sizeInMB="100" />
</LocalResources>
</WorkerRole>
Values provided to execute the sample against local Azure Storage emulator
<Role name="HostWorker">
<Instances count="1" />
<ConfigurationSettings>
<Setting name="HostWorker.ConnectionString"
value="UseDevelopmentStorage=true" />
<Setting name="Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString"
value="UseDevelopmentStorage=true" />
</ConfigurationSettings>
</Role>