如何修改在Azure函数中本地注入的IConfiguration [英] How to modify IConfiguration natively injected in Azure Functions

查看:139
本文介绍了如何修改在Azure函数中本地注入的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获取配置(我们正在尝试

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门户中对其进行配置. 关键文件库文档.这样,azure函数配置机制不知道/不在意它在哪里运行,如果在本地运行,它将从local.settings.json加载,如果已部署,则它将从Azure应用配置中获取值,并且如果您需要Azure密钥保管库集成,则全部通过Azure密钥保管库引用完成.
  5. 我认为这也是关键,Azure函数配置与使用appsettings.json的传统.NET应用程序不同.
  6. 配置azure功能应用程序会变得很麻烦,因为您需要一个接一个地添加设置.我们通过使用 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函数中本地注入的IConfiguration的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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