0

当我在函数中注入 IConfiguration 时,它找不到任何只存在于我的“Azure 应用程序配置”中的键。

我有一个使用 DefaultAzureCredential 访问应用程序配置的 functionApp (V3)。我在调试中本地运行它,因此需要默认凭据。我也有多个租户,所以我必须在 DefaultAzureCredentialOptions 上设置 VisualStudioTenantId 和 SharedTokenCacheTenantId。我的 Visual Studio 用户还被赋予了“App Configuration Data Reader”角色,以便能够进行调试。

连接到应用程序配置时,我没有收到任何错误。已编辑添加:我已设置 AppConfiguration 以使用 AzureAD 进行身份验证。

请参见下面的代码:

public override async void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)
{
    var credOptions = new DefaultAzureCredentialOptions();

    var tenantId = Environment.GetEnvironmentVariable("Tenant_Id");

    credOptions.VisualStudioTenantId = tenantId;
    
    credOptions.SharedTokenCacheTenantId = tenantId;

    var cred = new DefaultAzureCredential(credOptions);

    /*Works but requires SharedTokenCacheTenantId*/
    var secretClient = new SecretClient(new Uri(vaultURI), cred);
    var secret = await secretClient.GetSecretAsync("<secret name>");

    /*Works but where are my keys when I try to access them?*/
    builder.ConfigurationBuilder.AddAzureAppConfiguration(options =>
    {
        options.Connect(new Uri(appConfigURI), cred);
        
    }).Build(); //Should I be building this??

}

在我的功能中

public FunctName(IConfiguration configuration)
{
    _configuration = configuration;
}

当我访问该物业时

var prop = _configuration["PropertyName"];
4

2 回答 2

0

注意async void ConfigureAppConfiguration。这导致我的 ConfigureAppConfiguration 无法同步执行,导致 configure 在填充之前添加​​我的应用程序配置。

于 2021-03-19T06:54:36.540 回答
0

这里有一个使用 IFunctionsConfigurationBuilder 的示例函数应用程序https://github.com/Azure/AppConfiguration/blob/main/examples/DotNetCore/AzureFunction/FunctionApp/Startup.cs。我建议看一下,看看是否有遗漏的部分。

标题提到“在本地使用 DefaultAzureCredential”。这是否意味着如果您使用连接字符串,这将按预期工作?

于 2021-03-18T15:57:23.983 回答