# Using NServiceBus in Azure Functions (isolated worker) 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](https://learn.microsoft.com/en-us/cli/azure/install-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 --resource-group ``` The queue can also be automatically created by adding a call to the `configuration.AdvancedConfiguration.EnableInstallers()` method. ### Configure Connection string To use the sample, a valid Azure Service Bus connection string must be provided in the `local.settings.json` file. ## Sample structure The sample contains the following projects: - `AzureFunctions.ASBTrigger.Worker` - The Azure Functions host and NServiceBus endpoint - `AzureFunctions.Messages` - message definitions ## Running the sample The Functions project contains two functions: 1. An auto-generated Service Bus-triggered function. This function is generated by the `NServiceBusTriggerFunction` attribute in the `Program.cs` file. 1. 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: 1. Open a browser and navigate to `http://localhost:7071/api/HttpSender`. The port number might be different and will be indicated when the function project is started. 1. The queue-triggered function will receive the `TriggerMessage` and process it with NServiceBus. 1. The NServiceBus message handler for `TriggerMessage` sends a `FollowUpMessage`. 1. The queue-triggered function will receive the `FollowUpMessage` and process it with NServiceBus. ## Code walk-through The NServiceBus endpoint is configured as follows: ```cs [assembly: NServiceBusTriggerFunction("ASBWorkerEndpoint")] var builder = FunctionsApplication.CreateBuilder(args); builder.AddNServiceBus(); var host = builder.Build(); await 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: ```cs public class TriggerMessageHandler(ILogger logger) : IHandleMessages { public Task Handle(TriggerMessage message, IMessageHandlerContext context) { logger.LogWarning("Handling {MessageType} in {HandlerType}", nameof(TriggerMessage), nameof(TriggerMessageHandler)); return context.SendLocal(new FollowupMessage()); } } ``` ```cs public class FollowupMessageHandler(ILogger logger) : IHandleMessages { public Task Handle(FollowupMessage message, IMessageHandlerContext context) { logger.LogWarning("Handling {MessageType} in {HandlerType}.", nameof(FollowupMessage), nameof(FollowupMessageHandler)); return Task.CompletedTask; } } ```