首页 > 解决方案 > System.TypeLoadException:从负载测试插件使用 KeyVault 时的“方法 'get_SerializationSettings'

问题描述

我有一个Key Vault用于预先验证 Web 请求的负载测试。一旦代码尝试调用KeyVaultClient内部使用类的方法,就会抛出以下异常:

System.TypeLoadException:来自程序集“Microsoft.Azure.KeyVault,版本=3.0.0.0,文化=中性,PublicKeyToken=31bf3856ad364e35”的“Microsoft.Azure.KeyVault.KeyVaultClient”类型中的“方法“get_SerializationSettings”没有实现。

我试图将 KeyVault nuget 降级到 version 2.0.6,但我收到相同的错误,版本为 2.0.0.0。

我正在使用 .NET 框架 4.7.2 和 Visual Studio 2017 v.15.9.7

更新:当 nuget Microsoft.Rest.ClientRuntime nuget(由 Microsoft.Azure.KeyVault 引用)更新到版本 2.3.20 时,会出现此问题。如果我将其回滚到 v. 2.3.18,负载测试工作正常。

标签: .netazurevisual-studio-2017load-testingazure-keyvault

解决方案


这是我在使用 3.0.3 库访问 keyvault 客户端时在我的代码中使用的东西,它对我有用。在下面试试这个,看看它是否有效。

Uri ADL_TOKEN_AUDIENCE = new Uri(urlAudience);
var client = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(GetAccessToken));
public static async Task<string> GetAccessToken(string azureTenantId, string azureAppId, string azureSecretKey)
{
    var context = new AuthenticationContext(ConfigurationManager.AppSettings.Get("Authority") + tenantId);
    ClientCredential clientCredential = new ClientCredential(appId, secretKey);
    var tokenResponse = await context.AcquireTokenAsync(ConfigurationManager.AppSettings.Get("VaultUrl"), clientCredential);
    var accessToken = tokenResponse.AccessToken;
    return accessToken;
}

尝试以这种方式获取令牌,它应该可以工作。


推荐阅读