Using NServiceBus in Azure Functions (isolated process)

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<T> 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

Unlike a traditional NServiceBus endpoint, an endpoint hosted in Azure Functions cannot create its own input queue; the queue must be created elsewhere, for example 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>

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.
  2. 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.
  2. The queue-triggered function will receive the TriggerMessage and process it with NServiceBus.
  3. The NServiceBus message handler for TriggerMessage sends a FollowUpMessage.
  4. 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 void Main()
    {
        var host = new HostBuilder()
            .ConfigureFunctionsWorkerDefaults()
            .UseNServiceBus()
            .Build();

        host.Run();
    }
}

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;
    }
}

Related Articles

  • Isolated Worker
    Using NServiceBus with the Azure Functions isolated worker hosting model.

Last modified