如何将appsettings.json文件添加到我的Azure Function 3.0配置中? [英] How to add an appsettings.json file to my Azure Function 3.0 configuration?

查看:243
本文介绍了如何将appsettings.json文件添加到我的Azure Function 3.0配置中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新的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/zh-CN/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);
                                           });

但是这需要在主机环境中将所有设置添加为键/值 strings ,这是我不想做的.它们太多了,而且不像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函数工具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.

所以,我的问题是:如何从我的appsetting.json文件中读取的MachineLearningConfig部分作为IOption<MachineLearningConfig>注入到我的应用程序中?

So, my question: how to have my MachineLearningConfig section read from my appsetting.json file injected as IOption<MachineLearningConfig> in my app ?

推荐答案

Nkosi的解决方案效果很好,但它确实通过替换IConfiguration单例(services.AddSingleton<IConfiguration>)来更新了Azure函数运行时为其自身加载设置的方式.

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.我只需要注入链接到我自己的IConfigurationRoot的设置IOption<MachineLearningSettings>.

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配置中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆