我们需要add本地提供给 Azure Functions 的本机 IConfiguration 的配置提供程序。目前我们完全替换使用以下代码将其与我们的自定义 Iconfiguration 结合使用:
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 获取配置(我们正在尝试配置 https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-storage-queue-output?tabs=csharp#hostjson-settings队列触发器)。使用默认的 IConfiguration 正确读取 host.json 中的设置。
有一些关于使用 .NET Core Azure 函数的评论:
- 在本地运行时,框架默认为您加载 local.settings.json 。
- 您应该避免从 appsettings.json 或其他文件中读取配置值,特别是在消耗计划上运行时。函数文档 https://learn.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection#function-app-provided-services
- 一般来说,您应该避免传递
IConfiguration
目的。正如@Dusty提到的,更喜欢的方法是使用IOptions
图案。
- 如果您尝试从 Azure Key Vault 读取值,则无需添加
AddAzureKeyVault()
因为您可以并且应该使用 Azure Key Vault 引用在 azure 门户中进行配置。密钥库文档 https://learn.microsoft.com/en-us/azure/app-service/app-service-key-vault-references。通过这样做,azure函数配置机制不知道/关心它在哪里运行,如果您在本地运行,它将从local.settings.json加载,如果它被部署,那么它将从Azure应用程序配置中获取值并如果您需要 Azure Key Vault 集成,这一切都可以通过 Azure Key Vault 引用完成。
- 我认为这里的关键还在于 Azure 函数配置与使用 appsettings.json 的传统 .NET 应用程序不同。
- 配置azure功能应用程序可能会变得很麻烦,因为您需要一项一项地添加设置。我们通过使用解决了这个问题Azure 应用程序配置。 https://learn.microsoft.com/en-us/azure/azure-app-configuration/overview您也可以将其连接到 Azure Key Vault。
- Application Insights 由 Azure Functions 自动添加。Docs https://learn.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection#logging-services
话虽如此,尽管不建议通过执行以下操作,您仍然可以完成您想要的事情。请记住,您还可以在以下代码中添加密钥保管库引用: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));
如果您需要更多的意见/说明,请告诉我,我将相应地更新我的答案。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)