2

I have an azure function that runs off of a queue trigger. The repository has method to grab the connection string from the ConnectionStrings collection.

return System.Configuration.ConfigurationManager.ConnectionStrings["MyDataBase"].ToString();

This works great for the most part but I see intermittently that this returns a null exception error.
Is there a way I can make this more robust? Do azure functions sometimes fail to get the settings? Should I store the setting in a different section? I also want to say that this runs thousands of times a day but I see this popup about a 100 times. Runtime version: 1.0.12299.0

2 Answers 2

1

Are you reading the configuration for every function call? You should consider reading it once (e.g. using a Lazy<string> and static) and reusing it for all function invocations.

Maybe there is a concurrency issue when multiple threads access the code. Putting a lock around the code could help as well. ConfigurationManager.ConnectionStrings should be tread-safe, but maybe it isn't in the V1 runtime.

A similar problem was posted here, but this concerned app settings and not connection strings. I don't think using CloudConfigurationManager should be the correct solution.

You can also try putting the connection string into the app settings, unless you are using Entity Framework.

Connection strings should only be used with a function app if you are using entity framework. For other scenarios use App Settings. Click to learn more. (via Azure Portal)

Not sure if this applies to the V1 runtime as well.

Sign up to request clarification or add additional context in comments.

3 Comments

So what I did was add a private static string to hold the connection string. when empty look it up from the connectionstring in the config. If that fails pause 1/2 second and retry up to 3 times. In the past 1 hour I have not seen this error popup. I will keep you posted and add the code as the answer.
Might be a known issue: github.com/Azure/azure-functions-host/issues/1577 - Could be that they only fixed it for the V2 runtime.
Alex AIT your answer led me down the path of the solution below. Thank you for your help
0

The solution was to add a private static string for the connection string. Then only read from the configuration if it fails. I then added a retry that paused half a second. This basically removed this from happening.

private static string connectionString = String.Empty;
    private string getConnectionString(int retryCount)
    {
        if (String.IsNullOrEmpty(connectionString))
        {
            if (System.Configuration.ConfigurationManager.ConnectionStrings["MyEntity"] != null)
            {
                connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["MyEntity"].ToString();
            }
            else
            {
                if (retryCount > 2)
                {
                    throw new Exception("Failed to Get Connection String From Application Settings");
                }
                retryCount++;
                getConnectionString(retryCount);
            }
        }
        return connectionString;
    }

I don't know if this perfect but it works. I went from seeing this exception 30 times a day to none.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.