Search code examples
azure-functionsazureservicebusdotnet-isolated

Isolated Azure functions failed to access Azure Service Bus


My Isolated Azure funtion is accessing Azure Service Bus. The Function App is working locally.

[Function("Function1")]
public async Task Run([ServiceBusTrigger("email", Connection = "AzureServiceBus:ConnectionString")] string myQueueItem, CancellationToken cancellationToken)        
    {
    }

Where appsettings.json has

"AzureServiceBus": {
    "ConnectionString": "Endpoint=sb://"
  }

Package Microsoft.Azure.Functions.Worker.Extensions.ServiceBus (5.11) is used for isolated model.

Program:

var host = new HostBuilder()
    .ConfigureFunctionsWorkerDefaults()
    .ConfigureAppConfiguration(builder =>
    {
        builder.AddJsonFile("local.settings.json");
        builder.AddJsonFile("appsettings.json");
        builder.AddUserSecrets<Program>();
    })    
    .ConfigureServices((context, services) => 
    {                
        services.Configure<AzureServiceBus>(context.Configuration.GetSection("AzureServiceBus"));
    })
    .Build();

host.Run();

However the Function App is not working on Azure infrastructure. The error:

The listener for function 'Functions.Function1' was unable to start. Service Bus account connection string 'AzureServiceBus:ConnectionString' does not exist. Make sure that it is a defined App Setting. Microsoft.Azure.WebJobs.Extensions.ServiceBus.Config.ServiceBusClientFactory.ResolveConnectionInformation

Not sure why Microsoft.Azure.WebJobs.Extensions.ServiceBus has been used here?

Microsoft.Azure.WebJobs.Host.Listeners.FunctionListenerException:
System.InvalidOperationException:
   at Microsoft.Azure.WebJobs.Extensions.ServiceBus.Config.ServiceBusClientFactory.ResolveConnectionInformation (Microsoft.Azure.WebJobs.Extensions.ServiceBus, Version=5.11.0.0, Culture=neutral, PublicKeyToken=92742159e12e44c8)
   at Microsoft.Azure.WebJobs.Extensions.ServiceBus.Config.ServiceBusClientFactory.CreateClientFromSetting (Microsoft.Azure.WebJobs.Extensions.ServiceBus, Version=5.11.0.0, Culture=neutral, PublicKeyToken=92742159e12e44c8)
   at System.Lazy`1.ViaFactory (System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Lazy`1.ExecutionAndPublication (System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Lazy`1.CreateValue (System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at Microsoft.Azure.WebJobs.ServiceBus.Listeners.ServiceBusListener+<>c__DisplayClass33_0.<.ctor>b__2 (Microsoft.Azure.WebJobs.Extensions.ServiceBus, Version=5.11.0.0, Culture=neutral, PublicKeyToken=92742159e12e44c8)
   at System.Lazy`1.ViaFactory (System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Lazy`1.CreateValue (System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at Microsoft.Azure.WebJobs.ServiceBus.Listeners.ServiceBusListener+<StartAsync>d__34.MoveNext (Microsoft.Azure.WebJobs.Extensions.ServiceBus, Version=5.11.0.0, Culture=neutral, PublicKeyToken=92742159e12e44c8)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at Microsoft.Azure.WebJobs.Host.Listeners.FunctionListener+<StartAsync>d__13.MoveNext (Microsoft.Azure.WebJobs.Host, Version=3.0.37.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35: D:\a\_work\1\s\src\Microsoft.Azure.WebJobs.Host\Listeners\FunctionListener.cs:68)

Solution

  • he listener for function 'Functions.Function1' was unable to start. Service Bus account connection string 'AzureServiceBus:ConnectionString' does not exist.

    Local.settings.json:

    {
        "IsEncrypted": false,
      "Values": {
        "AzureWebJobsStorage": "UseDevelopmentStorage=true",
        "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
        "connn": "Endpoint=sb://rithser.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=W9fTV0="
      }
    }
    

    Local Output:

    enter image description here

    After Deploying your function to Azure , you need to make sure that your connection string is present in App settings of Configuration Section. For me my connection connn is missing: enter image description here

    Now added connection string as below:

    enter image description here

    And you need to make sure that both(config and function) are same as below:

    enter image description here

    Now my Function is executed in Azure Portal.