azure-functionsazureservicebus

Function App ServiceBusTrigger with Service Bus Emulator locally


I have the service bus emulator running in a docker container. I'm able to send messages to this service bus successfully using code, but I'm unable to get function app service bus triggers to work. Specifically - I can't find the correct connection name in my config

// local.settings.json

{
  "Values": {
    "ServiceBusConnection__fullyQualifiedNamespace": "sb://localhost:5672"
  }
}
// Function1.cs

[Function(nameof(ServiceBusTest))]
public async Task ServiceBusTest(
    [ServiceBusTrigger(queueName: Queues.SmokeTest, Connection = "ServiceBusConnection")]
    RequestBase @event)
{
    await Task.CompletedTask;
}

This results in the following error

Exception: Azure.Messaging.ServiceBus.ServiceBusException: Connection refused ErrorCode: ConnectionRefused (ServiceCommunicationProblem)

I've tried a few values for my service bus connection config, but I've been getting this same error.

Here's some of the documentation for testing locally with the emulator https://learn.microsoft.com/en-us/azure/service-bus-messaging/test-locally-with-service-bus-emulator?tabs=automated-script


Solution

  • By specifying __fullqQualifiedNamespace you are directing the trigger to use Entra-based credentials for authorization with the emulator and a TLS-based connection, neither of which is supported. As a result, the emulator is refusing the connection.

    To work with the emulator, you must use a connection string. Adapting your example, the updated appsettings would look like:

    {
      "Values": {
        "ServiceBusConnection": "Endpoint=sb://localhost;SharedAccessKeyName=FakeKey;SharedAccessKey=FakeValue;UseDevelopmentEmulator=true;"
      }
    }
    

    The shared access key name and value are arbitrary, as they must be present but don't truly matter. The important part is ensuring the UseDevelopmentEmulator=true slug, which causes the connection to initiate without TLS for the emulator.