我们需要将配置提供程序添加到本地提供给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:
话虽如此,即使不建议执行以下操作,您仍然可以完成所需的操作.请记住,您还可以通过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 // github/Azure/Azure-Functions/issues/717 // Environment.CurrentDirectory doesn't cut it in the cloud. // stackoverflow/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
发布评论