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

    <legend id='q2eEe'><style id='q2eEe'><dir id='q2eEe'><q id='q2eEe'></q></dir></style></legend>

    1. <tfoot id='q2eEe'></tfoot>
    2. <small id='q2eEe'></small><noframes id='q2eEe'>

      如何修改在 Azure Functions 中本机注入的 IConfiguration

      时间:2023-10-05
      <legend id='Ff82G'><style id='Ff82G'><dir id='Ff82G'><q id='Ff82G'></q></dir></style></legend>
      <i id='Ff82G'><tr id='Ff82G'><dt id='Ff82G'><q id='Ff82G'><span id='Ff82G'><b id='Ff82G'><form id='Ff82G'><ins id='Ff82G'></ins><ul id='Ff82G'></ul><sub id='Ff82G'></sub></form><legend id='Ff82G'></legend><bdo id='Ff82G'><pre id='Ff82G'><center id='Ff82G'></center></pre></bdo></b><th id='Ff82G'></th></span></q></dt></tr></i><div id='Ff82G'><tfoot id='Ff82G'></tfoot><dl id='Ff82G'><fieldset id='Ff82G'></fieldset></dl></div>

          <bdo id='Ff82G'></bdo><ul id='Ff82G'></ul>

                  <tbody id='Ff82G'></tbody>
              • <tfoot id='Ff82G'></tfoot>
              • <small id='Ff82G'></small><noframes id='Ff82G'>

                本文介绍了如何修改在 Azure Functions 中本机注入的 IConfiguration的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我们需要将配置提供程序添加到本机提供给 Azure Functions 的本机 IConfiguration.目前,我们正在使用以下代码将其完全替换为我们的自定义 Iconfiguration:

                We need to add configuration providers to the native IConfiguration that is supplied to the Azure Functions natively. Currently we are completely replacing it with our custom Iconfiguration using the following code:

                public class Startup : FunctionsStartup
                {
                    public override void Configure(IFunctionsHostBuilder builder)
                    {
                        ...
                
                        var configuration = new ConfigurationBuilder()
                            .SetBasePath(Directory.GetCurrentDirectory())
                            .AddAzureKeyVault(...)
                            .AddJsonFile("local.settings.json", true, true)
                            .AddEnvironmentVariables()
                            .Build();
                
                        builder.Services.AddSingleton<IConfiguration>(configuration);
                
                        builder.Services.AddSingleton<IMyService, MyService>();
                    }
                }
                

                一些背景

                MyService 的构造函数值需要来自 KeyVault 提供程序以及其他实例,如 Application Insights 等.如果我们保留默认的 IConfiguration,它就没有 KeyVault 值.如果我们使用工厂创建 MyService 实例,我们需要手动提供 App Insights 实例等.目前替换 IConfiguration 编译和函数运行.但它破坏了其他默认行为,例如不从 host.json 获取配置(我们正在尝试 配置 队列触发器).使用默认的 IConfiguration 可以正确读取 host.json 中的设置.

                MyService needs in its constructor values from the KeyVault provider and also other instances like Application Insights, etc. If we leave the default IConfiguration, it doesn't have the KeyVault values. If we create the MyService instance with a factory, we need to manually provide the App Insights instance, etc. Currently replacing the IConfiguration compiles and the function runs. But it breaks other default behavior like not taking the configurations from the host.json (we are trying to configure the queue trigger). Using the default IConfiguration correctly reads the settings from host.json.

                推荐答案

                有一些关于使用 .NET Core Azure 函数的评论:

                There's a couple of comments about using a .NET Core Azure functions:

                1. 在本地运行时,框架默认为您加载 local.settings.json.
                2. 在消耗计划上运行时,您应该避免从 appsettings.json 或其他文件中读取配置值.函数文档
                3. 一般来说,您应该避免传递 IConfiguration 对象.正如@Dusty 提到的,首选方法是使用 IOptions 模式.
                4. 如果您尝试从 Azure Key Vault 读取值,则无需添加 AddAzureKeyVault(),因为您可以并且应该使用 Azure Key Vault 在 azure 门户中进行配置参考.Key Vault 文档.通过这样做,azure 函数配置机制不知道/关心它在哪里运行,如果您在本地运行,它将从 local.settings.json 加载,如果已部署,那么它将从 Azure 应用程序配置中获取值并如果您需要 Azure Key Vault 集成,这一切都通过 Azure Key Vault 参考完成.
                5. 我认为 Azure 函数配置与使用 appsettings.json 的传统 .NET 应用程序不同也是关键.
                6. 配置 azure functions 应用可能会变得很麻烦,因为您需要一一添加设置.我们通过使用 Azure 应用配置解决了这个问题.您可以也可以将其连接到 Azure Key Vault.
                7. Application Insights 由 Azure Functions 自动添加.文档
                1. When running locally, local.settings.json is loaded for you by default by the framework.
                2. You should avoid reading config values from appsettings.json or other files specially when running on the Consumption plan. Functions docs
                3. In general, you should refrain from passing around the IConfiguration object. As @Dusty mentioned, the prefer method is to use the IOptions pattern.
                4. If you're trying to read values from Azure Key Vault, you don't need to add the AddAzureKeyVault() since you can and should configure this in the azure portal by using Azure Key Vault References. Key Vault Docs. By doing this, the azure function configuration mechanism doesn't know/care where it's running, if you run locally, it will load from local.settings.json, if it's deployed, then it will get the values from the Azure App Configuration and if you need Azure Key Vault integration it's all done via Azure Key Vault references.
                5. I think it's also key here that Azure functions configuration are not the same as a traditional .NET application that uses appsettings.json.
                6. It can become cumbersome to configure the azure functions app since you need to add settings one by one. We solved that by using Azure App Configuration. You can hook it up to Azure Key Vault too.
                7. Application Insights is added by Azure Functions automatically. Docs

                话虽如此,即使不建议执行以下操作,您仍然可以完成您想要的事情.请记住,您还可以通过 AddAzureKeyVault()

                That being said, you can still accomplish what you want even though it's not recommend by doing the following. Keep in mind that you can also add the key vault references in the following code by AddAzureKeyVault()

                var configurationBuilder = new ConfigurationBuilder();
                var descriptor = builder.Services.FirstOrDefault(d => d.ServiceType == typeof(IConfiguration));
                if (descriptor?.ImplementationInstance is IConfiguration configRoot)
                {
                    configurationBuilder.AddConfiguration(configRoot);
                }
                
                // Conventions are different between Azure and Local Development and API
                // https://github.com/Azure/Azure-Functions/issues/717
                // Environment.CurrentDirectory doesn't cut it in the cloud.
                // https://stackoverflow.com/questions/55616798/executioncontext-in-azure-function-iwebjobsstartup-implementation
                var localRoot = Environment.GetEnvironmentVariable("AzureWebJobsScriptRoot");
                var actualRoot = $"{Environment.GetEnvironmentVariable("HOME")}/site/wwwroot";
                var basePath = localRoot ?? actualRoot;
                var configuration = configurationBuilder
                    .SetBasePath(basePath)
                    .AddJsonFile("local.settings.json", optional: true, reloadOnChange: false)
                    .AddEnvironmentVariables()
                    .Build();
                
                builder.Services.Replace(ServiceDescriptor.Singleton(typeof(IConfiguration), configuration));
                

                如果您需要更多输入/澄清,请告诉我,我会相应地更新我的答案.

                Let me know if you need more input/clarifications on this and I'll update my answer accordingly.

                这篇关于如何修改在 Azure Functions 中本机注入的 IConfiguration的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:Azure 函数,EF Core,无法加载 ComponentModel.Annotations 4.2.0.0 下一篇:在本地运行一次计时器触发的 Azure 函数的最简单方法是什么?

                相关文章

                • <bdo id='zHl71'></bdo><ul id='zHl71'></ul>

                <tfoot id='zHl71'></tfoot>
                <legend id='zHl71'><style id='zHl71'><dir id='zHl71'><q id='zHl71'></q></dir></style></legend>

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

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