azureconfigurationazure-functionscsx

Where to store application specific configuration properties in an Azure Functions app


I've inherited some code written for Azure Functions, and I know next to nothing about Azure Functions. Right now this code is written for our staging environment, and has things like hard coded URLs specific to our staging environment and various API keys that will be different in production.

The first task I'd like to accomplish is to get rid of these hard coded strings and move them into some sort of "configuration" file that can be deployed through our build system. In other words, the staging Azure Functions app will target our staging environment and the production app will target our production environment. With a normal C# app, I'd just stick this stuff in the web.config or app.config file.

I have two questions:

First: Where is the recommended place to store these configuration properties? Let's take this bit of code I have in my CSX file:

public const string apikey = "37eb882ff6914b4c9205673547c10dc8"; // This value needs to change based on our environment

The first thing I looked into was the Function app settings->Configure app settings->App settings section. I added a new App setting, but couldn't find any way to actually read the value in code (I tried using ConfigurationManager), nor could I find documentation on how this works.

Next thing I looked at was the function.json file, which contains binding information on how the function gets called. There doesn't appear to be an area in here where I can add my own settings.

Lastly, I looked at the host.json file which is at the root of the application. There's a bunch of documentation on this file, but it doesn't appear to be the appropriate place for custom settings.

Second: Once I add my property here, how do I read it using C# code? In other words, what would I replace that public const string line with?

Thanks so much!


Solution

  • The Function app settings -> Configure app settings -> App settings location is the recommended place to store the configuration settings.

    To access the App Settings from C# code, you can just use the ConfigurationManager class:

    public static readonly string value = System.Configuration.ConfigurationManager.AppSettings["MySetting"];
    

    Instead of using "const", you'll need to use "static readonly" and it'll function pretty much the same.