Azure KeyVaultClient Hyak.Common例外尝试获取访问令牌

时间:2016-11-15 07:26:41

标签: c# .net azure

尝试创建KeyVaultClient时遇到问题。我使用示例代码

    async Task<string> GetAccessToken(string authority, string resource, string scope)
    {
        var clientId = "ID";
        var clientSecret = "SECRET";
        ClientCredential clientCredential = new ClientCredential(clientId, clientSecret);

        var context = new AuthenticationContext(authority, TokenCache.DefaultShared);
        var result = await context.AcquireTokenAsync(resource, clientCredential);

        return result.AccessToken;
    }

    public async void Test()
    {
        var vaultAddress = "url";
        KeyVaultClient keyVaultClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(GetAccessToken));
    }

当我尝试创建keyVaultClient时,我收到异常

  

类型&#39; System.TypeInitializationException&#39;的例外情况发生在   Hyak.Common.dll但未在用户代码中处理

     

附加信息:类型初始化程序   &#39; Hyak.Common.ServiceClient`1&#39;抛出异常。

当我转到异常详细信息时,我可以看到

  

{&#34;无法加载文件或程序集&#39; Microsoft.Threading.Tasks,   Version = 1.0.12.0,Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a&#39;要么   其中一个依赖项。系统找不到该文件   指定。&#34;:&#34; Microsoft.Threading.Tasks,版本= 1.0.12.0,   文化=中立,PublicKeyToken = b03f5f7f11d50a3a&#34;}

     

我的packages.config:

 <package id="Hyak.Common" version="1.1.0" targetFramework="net46" />  
 <package id="Microsoft.Azure.Common" version="2.0.4"      targetFramework="net46" />
 <package id="Microsoft.Azure.Common.Dependencies" version="1.0.0"      targetFramework="net46" />
 <package id="Microsoft.Azure.KeyVault" version="1.0.0" targetFramework="net46" />
 <package id="Microsoft.Azure.SqlDatabase.ElasticScale.Client" version="1.3.2" targetFramework="net46" />
 <package id="Microsoft.Bcl" version="1.1.10" targetFramework="net46" />
 <package id="Microsoft.Bcl.Async" version="1.0.168" targetFramework="net46" />   
 <package id="Microsoft.Bcl.Build" version="1.0.21" targetFramework="net46" />
 <package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="3.13.7" targetFramework="net46" />
 <package id="Microsoft.Net.Http" version="2.2.29" targetFramework="net46" userInstalled="true" />
 <package id="Newtonsoft.Json" version="9.0.1" targetFramework="net46"/>

2 个答案:

答案 0 :(得分:0)

也许您错过了Microsoft.Bcl.Async也许您可以展示您的package.config以查看您已安装的软件包。 如果您缺少Microsoft.Bcl.Async,那么只需使用软件包管理器控制台安装它: Install-Package Microsoft.Bcl.Async

答案 1 :(得分:0)

在我的情况下,我错过了Microsoft.Threading.Tasks.dll的引用,Microsoft.Bcl.Async nuget包中包含它。