AZFunc and Azure Bus: The listener for function was unable to start

Claude VERNIER 61 Reputation points
2024-03-14T16:10:32.9233333+00:00

I'm working on an Azure Function triggered by a queue on a Service bus.

When working on localhost, the function never gets triggered but there is no error.

On Azure, I get from the logs: The listener for function 'Functions.Receive' was unable to start.

My function looks like:

	[Function("Receive")]
	public string Receive([ServiceBusTrigger("acme", Connection = "POC_BUS")] string mySbMsg)
	{
		string msg;
		try
		{
			msg = "Message received = " + mySbMsg;
		}
		catch (Exception ex)
		{
			msg = ex.Message;
		}
		return msg;
	}

My local.settings.json file has:

	{
	  "IsEncrypted": false,
	  "POC_BUS:fullyQualifiedNamespace": "myacmebus.servicebus.windows.net",
	  "POC_BUS__fullyQualifiedNamespace": "myacmebus.servicebus.windows.net",
	  "WEB_PROXY": "http://myproxy.acme.com:443",
	
	  "Values": {
	    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
	    "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
	    "APPINSIGHTS_INSTRUMENTATIONKEY": "21a...3bd",
	
	    "POC_BUS:fullyQualifiedNamespace": "myacmebus.servicebus.windows.net",
	    "POC_BUS__fullyQualifiedNamespace": "myacmebus.servicebus.windows.net",
	    "WEB_PROXY": "http://myproxy.acme.com:443",
	  }
	}

I'm working with VS2022, a .Net 7 project with these dependencies:

  <PropertyGroup>
    <TargetFramework>net7.0</TargetFramework>
    <AzureFunctionsVersion>v4</AzureFunctionsVersion>
    <OutputType>Exe</OutputType>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Azure.Identity" Version="1.10.4" />
    <PackageReference Include="Azure.Messaging.ServiceBus" Version="7.17.4" />
    <PackageReference Include="Azure.Security.KeyVault.Secrets" Version="4.6.0" />
    <PackageReference Include="Microsoft.ApplicationInsights" Version="2.22.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.14.1" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.0.13" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.ServiceBus" Version="5.7.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.10.0" />
    <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
  </ItemGroup>

I could extract those logs:

Job host stopped
Stopping JobHost
The listener for function 'Functions.Receive' was unable to start.
Retrying to start listener for function 'Functions.Receive' (Attempt 10)
...
Retrying to start listener for function 'Functions.Receive' (Attempt 2)
Host lock lease acquired by instance ID '53...c7'.
Retrying to start listener for function 'Functions.Receive' (Attempt 1)
Worker process started and initialized.
Host started (608ms)
Job host started
"HttpOptions
{
  ""DynamicThrottlesEnabled"": false,
  ""EnableChunkedRequestBinding"": false,
  ""MaxConcurrentRequests"": -1,
  ""MaxOutstandingRequests"": -1,
  ""RoutePrefix"": ""api""
}"
Host initialized (237ms)
"Initializing function HTTP routes
Mapped function route 'api/Hello' [get,post] to 'Hello'
Mapped function route 'api/Post' [get,post] to 'Post'
Mapped function route 'api/Read' [get,post] to 'Read'
Mapped function route 'api/ReadAzure' [get,post] to 'ReadAzure'
Mapped function route 'api/Write' [get,post] to 'Write'
Mapped function route 'api/WriteAzure' [get,post] to 'WriteAzure'"
The 'AutoCompleteMessages' option has been overriden to 'True' value for 'Functions.Receive' function.
"Found the following functions:
Host.Functions.Hello
Host.Functions.Post
Host.Functions.Read
Host.Functions.ReadAzure
Host.Functions.Receive
Host.Functions.Write
Host.Functions.WriteAzure"
Generating 7 job function(s)
7 functions loaded
2 functions found (Custom)
Reading functions metadata (Custom)
Loading functions metadata
Starting Host (HostId=myFunc, InstanceId=16...831, Version=4.30.0.22097, ProcessId=nnnn, AppDomainId=1, InDebugMode=False, InDiagnosticMode=False, FunctionsExtensionVersion=~4)
Starting JobHost

From Azure portal:

I granted bus receiver role to my account and to azure function managed identity.

In console window of the azure function, I did "curl https://myacmebus.servicebus.windows.net/acme" and got a red message showing the amount of messages received and xferd and average speed so it seems the function has access.

I defined: POC_BUS/fullyQualifiedNamespace, POC_BUS:fullyQualifiedNamespace and POC_BUS__fullyQualifiedNamespace with  "myacmebus.servicebus.windows.net"

I really have two issues as it doesn't work either on local or on Azure but the one on Azure is my prioriy now.

If you could help, I'd appreciate as I have no idea what to try anymore.

Claude

Azure Service Bus
Azure Service Bus
An Azure service that provides cloud messaging as a service and hybrid integration.
635 questions
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,090 questions
0 comments No comments
{count} votes

Accepted answer
  1. Sina Salam 12,011 Reputation points
    2024-03-29T19:57:43.57+00:00

    Hello Claude VERNIER ,

    Welcome to the Microsoft Q&A and thank you for posting your questions here.

    I understand that your are facing issues with an Azure Function triggered by a Service Bus queue, both locally and on Azure. The local environment fails to trigger the function, while the Azure deployment encounters a listener startup failure. Despite correct configurations and connectivity to the Service Bus, the function fails to start, indicating potential configuration mismatches or compatibility issues between the local and Azure environments.

    To solve this issues:

    Open the local.settings.json file and ensure that the Service Bus connection string under the "POC_BUS" key is correct. Verify that all necessary dependencies and configurations are correctly set up in the Azure Function project. Below is an example of how you might configure the local.settings.json file for an Azure Function triggered by a Service Bus queue:

    {
      "IsEncrypted": false,
      "Values": {
        "AzureWebJobsStorage": "UseDevelopmentStorage=true",
        "FUNCTIONS_WORKER_RUNTIME": "dotnet",
        "POC_BUS": "Endpoint=sb://your-service-bus-namespace.servicebus.windows.net/;SharedAccessKeyName=YourSharedAccessKeyName;SharedAccessKey=YourSharedAccessKey"
      }
    }
    

    Replace "YourServiceBusConnectionString" with your actual Service Bus connection string.

    Secondly, add logging statements within the function code using a logging framework like Serilog or Microsoft.Extensions.Logging. Run the Azure Function project in debug mode using Visual Studio and analyze the logs to identify any potential issues with function execution.

    Thirdly, verify permissions granted to your account and the Azure Function managed identity for accessing the Service Bus. Compare the Azure Function App's configuration with the local.settings.json file to ensure consistency, especially regarding the Service Bus connection string.

    Finally, use Azure Service Bus Explorer or similar tools to verify connectivity to the Service Bus namespace from both local and Azure environments. Ensure that the Service Bus queue or topic is correctly configured to trigger the Azure Function.

    I hope this is helpful! Do not hesitate to let me know if you have any other questions.

    Please remember to "Accept Answer" if answer helped, so that others in the community facing similar issues can easily find the solution.

    Best Regards,

    Sina Salam

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.