Adds ASP.NET Core Configuration capabilities to Azure Functions
Azure Functions typically relies on local.settings.json and Azure environment variables for appsettings. If you are here, you want an experience similar to ASP.NET.
To configure this plugin, add a NuGet reference to AzureFunctions.AspNetConfiguration
and then add builder.UseAspNetConfiguration()
to Startup.cs, like:
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using YellowCounter.AzureFunctions.AspNetConfiguration;
[assembly: FunctionsStartup(typeof(SampleFunctionApp.Startup))]
namespace SampleFunctionApp
{
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder.UseAspNetConfiguration();
}
}
}
Unlike local.settings.json, appsettings.json supports complex/sectioned settings such as
{
"section": {
"hello": "world"
},
}
They are retrieved via standard means, as one would expect.
The configuration added ASP.NET configuration on top of the default Azure Functions configuration settings. So we get this hierarchy:
- local.settings.json (by default)
- Environment variables (by default)
- appsettings.json
- appsettings.{EnvironmentName}.json
- EnvironmentName is pulled from the value if
AZURE_FUNCTIONS_ENVIRONMENT
- In Visual Studio, this value defaults to 'Development'
- In Azure, this value defaults to 'Production'
- I'm following up to determine how to best override this value locally.
- You can override using
ASPNETCORE_ENVIRONMENT
, but it is not recommended
- User Secrets, if
AZURE_FUNCTIONS_ENVIRONMENT
is set to 'Development' - Environment variables (again, to give it the highest priority)
If you would like to use other ASP.NET config providers, you can add others to the end (and thus give them highest priority), for example:
var settings = new Dictionary<string, string>
{
{ "setting5", "memory" }
};
builder.UseAspNetConfiguration(c => c.AddInMemoryCollection(settings).AddEnvironmentVariables());
In order to retrieve the configuration settings themselves, you can use:
var configuration = builder.GetConfiguration();
var value = configuration["setting1"];