<tfoot id='TPCqr'></tfoot>

    <i id='TPCqr'><tr id='TPCqr'><dt id='TPCqr'><q id='TPCqr'><span id='TPCqr'><b id='TPCqr'><form id='TPCqr'><ins id='TPCqr'></ins><ul id='TPCqr'></ul><sub id='TPCqr'></sub></form><legend id='TPCqr'></legend><bdo id='TPCqr'><pre id='TPCqr'><center id='TPCqr'></center></pre></bdo></b><th id='TPCqr'></th></span></q></dt></tr></i><div id='TPCqr'><tfoot id='TPCqr'></tfoot><dl id='TPCqr'><fieldset id='TPCqr'></fieldset></dl></div>

    <small id='TPCqr'></small><noframes id='TPCqr'>

    <legend id='TPCqr'><style id='TPCqr'><dir id='TPCqr'><q id='TPCqr'></q></dir></style></legend>
    • <bdo id='TPCqr'></bdo><ul id='TPCqr'></ul>

      如何将 appsettings.json 文件添加到我的 Azure Function 3.0 配置?

      时间:2023-10-05
        <legend id='vj1JW'><style id='vj1JW'><dir id='vj1JW'><q id='vj1JW'></q></dir></style></legend>
          <tbody id='vj1JW'></tbody>
        <tfoot id='vj1JW'></tfoot>

          <i id='vj1JW'><tr id='vj1JW'><dt id='vj1JW'><q id='vj1JW'><span id='vj1JW'><b id='vj1JW'><form id='vj1JW'><ins id='vj1JW'></ins><ul id='vj1JW'></ul><sub id='vj1JW'></sub></form><legend id='vj1JW'></legend><bdo id='vj1JW'><pre id='vj1JW'><center id='vj1JW'></center></pre></bdo></b><th id='vj1JW'></th></span></q></dt></tr></i><div id='vj1JW'><tfoot id='vj1JW'></tfoot><dl id='vj1JW'><fieldset id='vj1JW'></fieldset></dl></div>

        1. <small id='vj1JW'></small><noframes id='vj1JW'>

                <bdo id='vj1JW'></bdo><ul id='vj1JW'></ul>
                本文介绍了如何将 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/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 配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:Azure 函数 - 函数应该写在静态类中 下一篇:在我的 Azure Function 中引用 c# 类库

                相关文章

                  <bdo id='DLttx'></bdo><ul id='DLttx'></ul>
                1. <tfoot id='DLttx'></tfoot>
                2. <i id='DLttx'><tr id='DLttx'><dt id='DLttx'><q id='DLttx'><span id='DLttx'><b id='DLttx'><form id='DLttx'><ins id='DLttx'></ins><ul id='DLttx'></ul><sub id='DLttx'></sub></form><legend id='DLttx'></legend><bdo id='DLttx'><pre id='DLttx'><center id='DLttx'></center></pre></bdo></b><th id='DLttx'></th></span></q></dt></tr></i><div id='DLttx'><tfoot id='DLttx'></tfoot><dl id='DLttx'><fieldset id='DLttx'></fieldset></dl></div>

                    <legend id='DLttx'><style id='DLttx'><dir id='DLttx'><q id='DLttx'></q></dir></style></legend>
                  1. <small id='DLttx'></small><noframes id='DLttx'>