One of my clients wants to migrate an old .NET Framework 4.5.2 console app to an Azure function.
Sadly, this console app references a class library developed in .NET Framework 4.5.2 and they don't want to migrate it to .Net Core or modifying it in any way. So, the function will be in V1, which is the only supported version for .NET Framework.
The problem is that, internally, the class library uses the old ConfigurationManager
class in order to retrieve settings. To test if I can use the library, I have added a reference to System.Configuration
in the Azure function V1 project and tried to retrieve values from the local.settings.json
:
ConfigurationManager.AppSettings["Key"]
But this returns null.
I know that the proper way to retrieve settings in an Azure function is this one:
Environment.GetEnvironmentVariable("Key")
But, as I said before, I'm stuck with the old .NET Framework library and I need a way of getting values from local.settings.json
using ConfigurationManager.AppSettings
. Is it possible?
From Azure Function v2 onward we use ConfigurationBuilder instead of ConfigurationManager. But the Azure Functions v1 uses the ConfigurationManager for accessing the Application Setting or Environmental Variables. So using ConfigurationManager is the only option we have for Azure Function v1.
The example below shows the correct way to fetch the values -
...
var myclientId = System.Configuration.ConfigurationManager.AppSettings["ClientId"];
var myclientSecret = System.Configuration.ConfigurationManager.AppSettings["ClientSecret"];
...
ConfigurationManager is the good way of getting the settings you need. It exposes AppSettings, which is just a NameValueCollection – with a key (or “name”), you get back a string-typed value.
Also make sure your local.settings.json looks as follows -
{
"IsEncrypted":false,
"Values" : {
"MyUrl" : "www.google.com",
...
}
...
}
Check this discussion for more information. Note that if you are still in v1, GetEnvironmentVariable
can't get ConnectStrings as they are not imported into environment, only Values are available.