This sample shows how to host NServiceBus within an Azure Function. In this case, a function is triggered by an incoming Azure Service Bus message. This enables hosting message handlers in Azure Functions, gaining the abstraction of message handlers implemented using IHandleMessages
and taking advantage of NServiceBus's extensible message-processing pipeline.
When hosting NServiceBus within an Azure Function, the function hosts an NServiceBus endpoint that is capable of processing different message types.
Prerequisites
Create queue
To use the sample, the endpoint queue must be created. It can be done manually in the Azure portal, or with the Azure CLI. In this sample, the queue name is ASBWorkerEndpoint
.
To create the queue with the Azure CLI, execute the following Azure CLI command:
az servicebus queue create --name ASBWorkerEndpoint --namespace-name <asb-namespace-to-use> --resource-group <resource-group-containing-namespace>
The queue can also be automatically created by adding a call to the configuration.
method.
Configure Connection string
To use the sample, a valid Azure Service Bus connection string must be provided in the local.
file.
Sample structure
The sample contains the following projects:
AzureFunctions.
- The Azure Functions host and NServiceBus endpointASBTrigger. Worker AzureFunctions.
- message definitionsMessages
Running the sample
The Functions project contains two functions:
- An auto-generated Service Bus-triggered function. This function is generated by the
NServiceBusTriggerFunction
attribute in theProgram.
file.cs - An HTTP-triggered function to send a message to the Azure Service Bus queue.
Running the sample will launch the Azure Functions runtime window.
To try the Azure Function:
- Open a browser and navigate to
http:/
. The port number might be different and will be indicated when the function project is started./ localhost:7071/ api/ HttpSender - The queue-triggered function will receive the
TriggerMessage
and process it with NServiceBus. - The NServiceBus message handler for
TriggerMessage
sends aFollowUpMessage
. - The queue-triggered function will receive the
FollowUpMessage
and process it with NServiceBus.
Code walk-through
The NServiceBus endpoint is configured using IHostBuilder
as follows:
[assembly:NServiceBusTriggerFunction("ASBWorkerEndpoint")]
public class Program
{
public static Task Main()
{
var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults()
.UseNServiceBus()
.Build();
return host.RunAsync();
}
}
Note that NServiceBusTriggerFunction
is used to automatically generate the Azure Functions trigger code needed to invoke NServiceBus.
Handlers
These are the message handlers, with a CustomDependency
passed in.
public class TriggerMessageHandler : IHandleMessages<TriggerMessage>
{
static readonly ILog Log = LogManager.GetLogger<TriggerMessageHandler>();
public Task Handle(TriggerMessage message, IMessageHandlerContext context)
{
Log.Warn($"Handling {nameof(TriggerMessage)} in {nameof(TriggerMessageHandler)}");
return context.SendLocal(new FollowupMessage());
}
}
public class FollowupMessageHandler : IHandleMessages<FollowupMessage>
{
static readonly ILog Log = LogManager.GetLogger<FollowupMessageHandler>();
public Task Handle(FollowupMessage message, IMessageHandlerContext context)
{
Log.Warn($"Handling {nameof(FollowupMessage)} in {nameof(FollowupMessageHandler)}.");
return Task.CompletedTask;
}
}