尝试使用 .Net Framework 连接到 Azure App Configuration 时的奇怪行为
Strange behavior when trying to connect to Azure App Configuration with .Net Framework
尝试从 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();
}
此代码片段在应用程序启动期间执行时有效,例如路由配置。所有后续调用都正常,刷新也没有问题。
问题是在稍后第一次调用 App Configuration 时出现的,例如在请求期间。代码挂起,没有错误消息。在我的特殊情况下,我无法在启动时连接到 Azure 应用程序配置。
使用 .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()
没有任何反应,请求也没有 return。
我卡在这一点上了。有人有解决这个问题的方法吗?谢谢。
此致,
统计
这是因为 Azure.Core
软件包版本 1.4.1 中引入了错误。该错误已在 Azure.Core
版本 1.9.0 中修复。由于您引用的是 Azure.Identity
1.3.0 版,因此您间接依赖于 Azure.Core
1.6.0 版。
可以通过添加对 Azure.Core v1.9.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();
}
此代码片段在应用程序启动期间执行时有效,例如路由配置。所有后续调用都正常,刷新也没有问题。
问题是在稍后第一次调用 App Configuration 时出现的,例如在请求期间。代码挂起,没有错误消息。在我的特殊情况下,我无法在启动时连接到 Azure 应用程序配置。
使用 .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()
没有任何反应,请求也没有 return。
我卡在这一点上了。有人有解决这个问题的方法吗?谢谢。
此致, 统计
这是因为 Azure.Core
软件包版本 1.4.1 中引入了错误。该错误已在 Azure.Core
版本 1.9.0 中修复。由于您引用的是 Azure.Identity
1.3.0 版,因此您间接依赖于 Azure.Core
1.6.0 版。
可以通过添加对 Azure.Core v1.9.0 或更高版本的显式依赖来解决您的问题。