Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connection string is sometimes not available in ServiceBusTrigger

I have an Azure Function with a service bus trigger:

public static async Task Run([ServiceBusTrigger(
    "%inputTopicName%",
    "%subscriptionName%",
    AccessRights.Manage,
    Connection = "connection")]string mySbMsg)

In 99.9% of the invocations, the trigger successfully resolves to a subscription on Azure Service Bus. But sometimes, I see the following error in my log:

Microsoft.Azure.WebJobs.Host.FunctionInvocationException: Exception while executing function: UptimeChecker ---> System.ArgumentException: The argument connectionString is null or white space.
Parameter name: connectionString
   at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.FunctionInvocationFilterInvoker.d__9.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.d__24.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.d__23.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.d__22.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
   at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.d__16.MoveNext()
   --- End of inner exception stack trace ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.d__16.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.d__13.MoveNext()

It seems that the service bus trigger cannot resolve the connection variable from settings.

I tried to add a debugging filter to my function, in order to find out what is going on:

public class DebuggingFilter : FunctionInvocationFilterAttribute
{
    public override Task OnExecutingAsync(FunctionExecutingContext executingContext, CancellationToken cancellationToken)
    {
        executingContext.Properties.Add("connection", ConfigurationManager.AppSettings["connection"]);
        executingContext.Properties.Add("inputTopicName", ConfigurationManager.AppSettings["inputTopicName"]);
        executingContext.Properties.Add("subscriptionName", ConfigurationManager.AppSettings["subscriptionName"]);
        return base.OnExecutingAsync(executingContext, cancellationToken);
    }
}

When an error is logged in my function, the properties added to FunctionExecutionContext are automatically added to the log message. The weird thing here is that in the scenario where Azure Functions throw this exception, the property values are resolved and shown in the log message.

What could be the cause of this? I've experienced multiple problems with resolving settings from Azure Functions, so maybe it's a general problem?

like image 736
ThomasArdal Avatar asked Feb 12 '19 07:02

ThomasArdal


People also ask

How do I add a connection string in Azure?

Get connection informationSign in to the Azure portal. Select SQL Databases from the left-hand menu, and select your database on the SQL databases page. Select Connection strings under Settings and copy the complete ADO.NET connection string. For Azure SQL Managed Instance copy connection string for public endpoint.

How do I read messages from service bus queue?

Open Queue. Select Service Bus Explorer (preview) under Settings. Write policy name and select the checkbox to listen and click Create. Select the policy name and copy Primary or Secondary Connection String.


2 Answers

I don't have a final answer to this question, but I do want to share some details with other people experiencing the same problem. The problem seems to be isolated to v1 consumption based functions only. After upgrading my function to Azure Functions v2 and utilizing the new configuration system available there, I no longer experience the problem above. Since v1 is no longer the current version, I do not want to spend more time debugging into this, when v2 seems to be working.

like image 124
ThomasArdal Avatar answered Oct 31 '22 11:10

ThomasArdal


According to this: you should be using the latest version 2.3.0 for nuget Microsoft.Azure.WebJobs.ServiceBus -Version 2.3.0

I would also check if it is an issue when running in consumption plan by hardcoding the connection to see if it is an issue reading the configuration of connection. And otherwise test it on app service plan.

like image 32
Anass Kartit Avatar answered Oct 31 '22 11:10

Anass Kartit