Search code examples
azureservicebusmasstransitazure-managed-identity

MassTransit: Cannot access Azure Service Bus using managed Identity


I have the following code in the Azure function start up

 builder.Services.AddScoped<ExampleConsumer>()
                .AddMassTransitForAzureFunctions(cfg =>
                {
                    cfg.AddConsumersFromNamespaceContaining<ConsumerNamespace>();
                }, "AzureWebJobsServiceBus");

Configuration:

AzureWebJobsServiceBus__fullyQualifiedNamespace:  <service_bus_name>.servicebus.windows.net

Function Signature:

public Task Run([ServiceBusTrigger(<topic_name>, <subscription_name>, Connection = "AzureWebJobsServiceBus")] string message, FunctionContext context)

Its throwing exception

System.ArgumentNullException: A connection string must be used for Azure Functions. (Parameter 'AzureWebJobsServiceBus')

What should be the proper way to configure MassTransit for Azure function to connect Azure Service Bus using managed identity?

I explored the mass transit source code and found that it is expecting connection string containing SharedAccessKeyName and SharedAccessKey otherwise throw exception. Any alternative way to configure ?

  • MassTransit.WebJobs.ServiceBus version 8.0.7
  • Azure Function v4 and .NET6

Solution

  • I have the following code in the Azure function start up

     builder.Services.AddScoped<ExampleConsumer>()
                    .AddMassTransitForAzureFunctions(cfg =>
                    {
                        cfg.AddConsumersFromNamespaceContaining<ConsumerNamespace>();
                    }, "AzureWebJobsServiceBus:MassTransit");
    

    Configuration:

    AzureWebJobsServiceBus:fullyQualifiedNamespace:  <service_bus_name>.servicebus.windows.net
    

    For MassTransit:

    AzureWebJobsServiceBus:MassTransit:Endpoint=sb://<service_bus_name>.servicebus.windows.net
    

    Function Signature:

    public Task Run([ServiceBusTrigger(<topic_name>, <subscription_name>, Connection = "AzureWebJobsServiceBus")] string message, FunctionContext context)
    

    I have to provide two connection strings for Azure Function trigger and MassTransit. The difference between these two for mass transit it starts with Endpoint=sb://