问题描述
新的 Azure Function 3.0 SDK 提供了一种实现 Startup 类的方法.它可以访问依赖注入提供的服务集合,我可以在其中添加自己的组件和第三方服务.
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)
{
...
我的第三方服务将大型结构作为参数,并且这些配置文件与二进制文件一起复制.我可以将它们复制到 appsettings.json 文件的一个小节中:
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 )
}
}
配置值根据部署环境更新.为此,我使用 Azure Devops 的文件转换任务:生产值与暂存和开发值不同.
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.
鉴于文档 https://docs.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection 加载这些选项的方法是:
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);
});
但这需要在主机环境中将所有设置添加为键/值字符串,这是我不想做的.它们太多了,不像json配置文件那么容易维护.
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.
我将 appsettings.json 与 host.json 一起复制.
I copied that appsettings.json alongside the host.json.
但是 Azure Function SDK 在启动时读取的 appsettings.json 文件不是我的应用程序的 appsettings.json 而是 Azure Function 工具的 appsettings.json.因此 configuration.GetSection("MachineLearningConfig")
返回空值,因为 Azure Function tools bin 文件夹中没有 appsettings.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.
所以,我的问题是:如何让我的 MachineLearningConfig
部分从我的 appsetting.json
文件中读取为 IOption
在我的应用?
So, my question: how to have my MachineLearningConfig
section read from my appsetting.json
file injected as IOption<MachineLearningConfig>
in my app ?
推荐答案
Nkosi 的解决方案运行良好,但它确实更新了 azure 函数运行时为自己加载设置的方式,通过替换 IConfiguration 单例:services.AddSingleton<IConfiguration>
.
Nkosi's solution works pretty well, but it does update the way the azure function runtime loads settings for itself, by replacing IConfiguration singleton: services.AddSingleton<IConfiguration>
.
我更喜欢使用另一个未注入的 IConfigurationRoot.我只需要注入我的设置 IOption<MachineLearningSettings>
链接到我自己的 IConfigurationRoot.
I prefer having another IConfigurationRoot that is not injected. I just need to inject my settings IOption<MachineLearningSettings>
that are linked to my own IConfigurationRoot.
我构建了另一个 IConfigurationRoot,它是 Startup 类的成员:
I build another IConfigurationRoot that is member of the Startup class:
public class Startup : FunctionsStartup
{
private IConfigurationRoot _functionConfig = null;
private IConfigurationRoot FunctionConfig( string appDir ) =>
_functionConfig ??= new ConfigurationBuilder()
.AddJsonFile(Path.Combine(appDir, "appsettings.json"), optional: true, reloadOnChange: true)
.Build();
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddOptions<MachineLearningSettings>()
.Configure<IOptions<ExecutionContextOptions>>((mlSettings, exeContext) =>
FunctionConfig(exeContext.Value.AppDirectory).GetSection("MachineLearningSettings").Bind(mlSettings) );
}
}
注意:连接字符串必须保留在应用程序设置中,因为触发器需要它来创建未启动的函数应用实例(在消费服务计划中).
Note: connection strings must remain in the application settings, because it is required by triggers to create an instance of the the function app that is not started (in a consumption service plan).
这篇关于如何将 appsettings.json 文件添加到我的 Azure Function 3.0 配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!