The new Azure Function 3.0 SDK provides a way to implement a Startup class. It gives access to the collection of services that are available by dependency injection, where I can add my own components and third-party services.
But I don't know how to use a configuration file.
[assembly: FunctionsStartup(typeof(MyNamespace.Startup))]
namespace MyNamespace
{
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
...
My third party services take large structures as parameter, and those configuration files are copied with binaries. I can copy them in a subsection of an appsettings.json file:
{
"MachineLearningConfig" : {
( about 50+ parameters and subsections )
}
}
Configuration values are updated according to the environment of deployment . I use Azure Devops's File Transform Task for that: production values are different from staging and dev values.
Given the documentation https://docs.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection the way to load those options is:
builder.Services.AddOptions<MachineLearningConfig>()
.Configure<IConfiguration>((settings, configuration) =>
{
configuration.GetSection("MachineLearningConfig").Bind(settings);
});
But that requires to add all settings as key/value strings in the host's environment, and that is what I do not want to do. There are too many of them and that is not as easy to maintain as in a json configuration file.
I copied that appsettings.json alongside the host.json.
But the appsettings.json file read at startup by the Azure Function SDK is not my application's appsettings.json but Azure Function tools's appsettings.json. So configuration.GetSection("MachineLearningConfig")
returns empty values as there is no appsettings.json file in the Azure Function tools bin folder.
So, my question: how to have my MachineLearningConfig
section read from my appsetting.json
file injected as IOption<MachineLearningConfig>
in my app ?
Add Json File After adding the file, right click on appsettings. json and select properties. Then set “Copy to Ouptut Directory” option to Copy Always. Add few settings to json file, so that you can verify that those settings are loaded.
appsettings. json is one of the several ways, in which we can provide the configuration values to ASP.NET core application. You will find this file in the root folder of our project. We can also create environment-specific files like appsettings.
The host. json metadata file contains configuration options that affect all functions in a function app instance. This article lists the settings that are available starting with version 2. x of the Azure Functions runtime.
In the startup class:
IConfigurationRoot config = new ConfigurationBuilder()
.SetBasePath(Environment.CurrentDirectory)
.AddJsonFile("someSettings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
Add a json file to you project that holds the settings. Note that local.settings.json is ignored/removed during deployment. (Name the file something else.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With