Getting Azure Function Connection Strings from Configurations

Settings
Share on facebook
Share on twitter
Share on linkedin

Getting Started

Azure functions are very practical and unexpensive. You can have your own serverless function in a matter of minutes, check them out if you haven’t yet.

Getting in context

Today I was running into an issue working with an Azure Function. The AZF had to connect to an SQL server database. To handle connection strings the best practice is to parametrize them using config files for local development and using the configuration tab in azure once published.

You should be able to access the value by calling the System.Environment.GetEnvironmentVariable method, passing the key you want to get. But, for some reason, I wasn’t able to get the connection string from the settings. It worked in my machine but it didn’t work on azure.

The Solution

I did some googling around and I found this Github issue thread. In it I found my answer. I’m leaving the code bellow:

The explanation

As it turns out, Azure has a specific convention for naming connection strings sets from the Azure Function configuration. The convention depends on the Database engine the connection is aiming.

From what I gathered this is the convention, the name of the connection string key will start with one of these depending on the type of DB specified:

  • MySQL: MYSQLCONNSTR_
  • SQLServer: SQLCONNSTR_
  • SQLAzure: SQLAZURECONNSTR_
  • Custom Connection : CUSTOMCONNSTR_

In short I would recommend one of two approaches from now on, either copy the functions from the solution or rename your connection string key names in your local settings file to match the Azure configuration convention.

That’s all folks, have a good one!

Did you like the post and would like to leave a reply? You can mention me on twitter @eatskolnikov so we can have a conversation about it

Share it with your friends

Share on facebook
Share on twitter
Share on linkedin

Want to get these post in your inbox?

Type your email down here and you will get a weekly digest of the post here

Other articles on the site

core

Handling Excel Files with NPOI

NPOI is a library for working with Office documents like Word and Excel. I mainly been using it for reading and writing Excel files so that’s what I’m going to write about. It works with .csv and .xlsx formats and is based on the Apache POI project for Java. It

Read More »
nuget package

Getting the user signature

This is a common feature requested for many applications in the corporate world or if you are working on some sort of payment processing application or a delivery app. You may need the user signature to confirm they have received a package, an order has been delivered or many other

Read More »