0

尝试使用 ASP.Net Web 应用程序中的 Azure 标识连接到 Azure 应用程序配置时,我遇到了一个奇怪的行为。当前环境是 .Net Framework 4.8,但在 4.7.2 和 4.6.1 上的行为是相同的。

我正在使用 Microsoft.Extensions.Configuration.AzureAppConfiguration 4.0.0 和 Azure.Identity 1.3.0。在本地开发期间,我使用服务主体(在环境中定义的 AZURE_TENANT_ID、AZURE_CLIENT_ID 和 AZURE_CLIENT_SECRET),并且在 Azure 上系统分配了应用服务的 ManagedIdentity。

private IConfiguration GetConfiguration(string label = null)
{
    TokenCredential credential = new DefaultAzureCredential();

    return new Microsoft.Extensions.Configuration.ConfigurationBuilder()
        .AddAzureAppConfiguration(options =>
        {
            options = options.Connect(
                    new Uri(Environment.GetEnvironmentVariable("APP_CONFIGURATION_ENDPOINT")), credential)
                .ConfigureKeyVault(kv => { kv.SetCredential(credential); });
            if (!string.IsNullOrEmpty(label))
            {
                options.Select(KeyFilter.Any, label);
            }
            else
            {
                options.Select(KeyFilter.Any, LabelFilter.Null);
            }
        })
        .Build();
}

此代码片段在应用程序启动期间执行时有效,例如 RouteConfig。所有后续调用都可以正常工作,刷新也可以正常工作。

问题在稍后第一次调用应用程序配置时开始,例如在请求期间。代码挂起,没有错误消息。在我的特殊情况下,我无法在启动时连接到 Azure App Configuration。

使用 .Net Core 时,一切都像魅力一样,但目前这不是一个选择。

这是日志。

在启动期间执行时:

Starting IIS Express ...
Successfully registered URL "http://localhost:5000/" for site "AppConfigTest3" application "/"
Registration completed for site "AppConfigTest3"
IIS Express is running.
Enter 'Q' to stop IIS Express
Loading configuration ...
Created DefaultAzureCredential: Azure.Identity.DefaultAzureCredential
Using Azure environment
Created ManagedIdentityCredential: Azure.Identity.DefaultAzureCredential
Connected via APP_CONFIGURATION_ENDPOINT: https://my-test-appconfig.azconfig.io
Before configure KeyVault credential
After configure KeyVault credential
Before executing Build()
EscalationMonitorInterval: 300
Created DefaultAzureCredential: Azure.Identity.DefaultAzureCredential
Using Azure environment
Created ManagedIdentityCredential: Azure.Identity.DefaultAzureCredential
Connected via APP_CONFIGURATION_ENDPOINT: https://my-test-appconfig.azconfig.io
Before configure KeyVault credential
After configure KeyVault credential
Before executing Build()
Azure Config loaded
Anforderung gestartet: "GET" http://localhost:5000/

如果在请求期间第一次调用,我会收到以下日志:

Starting IIS Express ...
Successfully registered URL "http://localhost:5000/" for site "AppConfigTest3" application "/"
Registration completed for site "AppConfigTest3"
IIS Express is running.
Enter 'Q' to stop IIS Express
Azure Config loaded
Anforderung gestartet: "GET" http://localhost:5000/
Response sent: http://localhost:5000/ with HTTP status 200.0
Anforderung gestartet: "GET" http://localhost:5000/Home/About
Loading configuration ...
Created DefaultAzureCredential: Azure.Identity.DefaultAzureCredential
Using Azure environment
Created ManagedIdentityCredential: Azure.Identity.DefaultAzureCredential
Connected via APP_CONFIGURATION_ENDPOINT: https://my-test-appconfig.azconfig.io
Before configure KeyVault credential
After configure KeyVault credential
Before executing Build()

什么都没有发生,请求也没有返回。

我被困在这一点上。有人有解决这个问题的方法吗?谢谢你。

问候, 斯塔蒂

4

1 回答 1

1

Azure.Core这是因为包版本 1.4.1中引入了一个错误。该错误已在Azure.Core版本 1.9.0 中修复。由于您引用的是Azure.Identity1.3.0 版本,因此您间接依赖于Azure.Core1.6.0 版本。

可以通过添加对Azure.Core v1.9.0或更高版本的显式依赖来解决您的问题。

于 2021-04-06T01:15:18.450 回答