我有一种从KeyVault检索值的方法,如下所示:
public async Task<string> GetSecretAsync(string secret)
{
var azureServiceTokenProvider = new AzureServiceTokenProvider();
var keyVaultClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback));
try
{
var retrievedSecret = await keyVaultClient.GetSecretAsync($"{VaultUrl}/secrets/{secret}");
return retrievedSecret.Value;
}
catch (Exception e)
{
if (e.Message.Contains("Forbidden"))
{
throw new KeyVaultForbiddenException("Keyvault request refused");
}
else if (e.Message.Contains("NotFound"))
{
throw new KeyVaultSecretNotFoundException("Requested secret not found");
}
Trace.WriteLine($"Exception getting secret: {e.Message}");
throw e;
}
}
在异步方法中可以正常工作。
现在我正尝试在同步方法(实际上是构造函数)中使用它,但它失败了-主要问题是我无法捕获异常来真正找出问题所在
这是我尝试使用它的方式:
public MongoRepository(IKeyVaultService KeyVaultService)
{
this.collectionName = typeof(T).Name;
var databaseName = KeyVaultService.GetSecretAsync("mongoDatabaseName").Result;
var databaseConnectionString = KeyVaultService.GetSecretAsync("mongoDatabaseConnectionString").Result;
SetDatabase(databaseName, databaseConnectionString);
SetCollection();
}
一旦代码移开:
var retrievedSecret = await keyVaultClient.GetSecretAsync($"{VaultUrl}/secrets/{secret}");
它失败了,我无法捕捉到异常。我可以在输出控制台中看到以下内容:
Exception thrown: 'Microsoft.Rest.TransientFaultHandling.HttpRequestWithStatusException' in Microsoft.Rest.ClientRuntime.dll
Exception thrown: 'Microsoft.Rest.TransientFaultHandling.HttpRequestWithStatusException' in mscorlib.dll
Exception thrown: 'System.Net.Sockets.SocketException' in System.dll
Exception thrown: 'System.Net.WebException' in System.dll
Exception thrown: 'System.Net.WebException' in System.dll
Exception thrown: 'System.Net.Http.HttpRequestException' in mscorlib.dll
Exception thrown: 'Microsoft.Azure.Services.AppAuthentication.AzureServiceTokenProviderException' in Microsoft.Azure.Services.AppAuthentication.dll
Exception thrown: 'Microsoft.Azure.Services.AppAuthentication.AzureServiceTokenProviderException' in mscorlib.dll
Exception thrown: 'Microsoft.Azure.Services.AppAuthentication.AzureServiceTokenProviderException' in mscorlib.dll
Exception thrown: 'System.Exception' in Microsoft.Azure.Services.AppAuthentication.dll
Exception thrown: 'Microsoft.Azure.Services.AppAuthentication.AzureServiceTokenProviderException' in Microsoft.Azure.Services.AppAuthentication.dll
Exception thrown: 'Microsoft.Azure.Services.AppAuthentication.AzureServiceTokenProviderException' in mscorlib.dll
Exception thrown: 'Microsoft.Azure.Services.AppAuthentication.AzureServiceTokenProviderException' in mscorlib.dll
Exception thrown: 'System.ObjectDisposedException' in System.dll
Exception thrown: 'System.ObjectDisposedException' in System.dll
Exception thrown: 'System.ObjectDisposedException' in System.dll
Exception thrown: 'System.Exception' in mscorlib.dll
Exception thrown: 'Microsoft.Azure.Services.AppAuthentication.AzureServiceTokenProviderException' in Microsoft.Azure.Services.AppAuthentication.dll
Exception thrown: 'Microsoft.Azure.Services.AppAuthentication.AzureServiceTokenProviderException' in mscorlib.dll
Exception thrown: 'Microsoft.Azure.Services.AppAuthentication.AzureServiceTokenProviderException' in mscorlib.dll
Exception thrown: 'Microsoft.IdentityModel.Clients.ActiveDirectory.AdalSilentTokenAcquisitionException' in Microsoft.IdentityModel.Clients.ActiveDirectory.dll
Exception thrown: 'Microsoft.IdentityModel.Clients.ActiveDirectory.AdalSilentTokenAcquisitionException' in mscorlib.dll
Exception thrown: 'Microsoft.IdentityModel.Clients.ActiveDirectory.AdalSilentTokenAcquisitionException' in Microsoft.IdentityModel.Clients.ActiveDirectory.dll
Exception thrown: 'Microsoft.IdentityModel.Clients.ActiveDirectory.AdalSilentTokenAcquisitionException' in mscorlib.dll
Exception thrown: 'Microsoft.IdentityModel.Clients.ActiveDirectory.AdalSilentTokenAcquisitionException' in mscorlib.dll
Exception thrown: 'Microsoft.IdentityModel.Clients.ActiveDirectory.AdalSilentTokenAcquisitionException' in mscorlib.dll
Exception thrown: 'Microsoft.IdentityModel.Clients.ActiveDirectory.AdalSilentTokenAcquisitionException' in mscorlib.dll
Exception thrown: 'Microsoft.IdentityModel.Clients.ActiveDirectory.AdalSilentTokenAcquisitionException' in mscorlib.dll
任何朝着正确方向指向的点都很棒!