背景:我已经在 Google Developer Console 上为我的项目设置了一个服务帐户,并使用服务帐户电子邮件、证书和秘密密码,并按照 GoogleAPisSample Plus.ServiceAccount 中提供的示例。下面的代码片段是我的 Windows 服务应用程序的一部分:
var List<string> Scopes = new List<string> { "https://www.googleapis.com/auth/analytics.readonly" };
var credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(ServiceAccountEmail)
{
Scopes = Scopes
}.FromCertificate(certificate));
if(credential.RequestAccessTokenAsync(CancellationToken.None).Result)
{
AuthenticationKey = credential.Token.AccessToken;
}
当我在本地开发机器上安装并运行此服务时,它会很好地执行 credential.RequestAccessTokenAsync 并接收 AccessToken 并且服务会继续并很好地读取分析数据。
但是,当它部署在我们的 QA 环境(Window Server 2008 R2 Standard)上并再次运行时,调用 credential.RequestAccessTokenAsync 时会引发以下异常:
System.AggregateException: One or more errors occurred. ---> System.MissingMethodException: Method not found: 'System.Net.HttpStatusCode System.Net.Http.HttpResponseMessage.get_StatusCode()'.
at Microsoft.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at Microsoft.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccess(Task task)
at OurApplication.SchedulerService.GoogleAnalytics.OAuth2.ServiceAccountCredential.<RequestAccessTokenAsync>d__b.MoveNext()
--- End of inner exception stack trace ---
at System.Threading.Tasks.Task`1.get_Result()
at OurApplication.SchedulerService.GoogleAnalytics.GADataFetcher.AuthenticateAndAuthorize()
at OurApplication.SchedulerService.GoogleAnalytics.GADataFetcher..ctor()
at OurApplication.SchedulerService.GoogleAnalytics.GoogleAnalyticsService.GoogleAnalyticsTopPerformances(Int32 sessID, String sessToken)
---> (Inner Exception #0) System.MissingMethodException: Method not found: 'System.Net.HttpStatusCode System.Net.Http.HttpResponseMessage.get_StatusCode()'.
at Microsoft.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at Microsoft.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccess(Task task)
at Seatwave.SchedulerService.GoogleAnalytics.OAuth2.ServiceAccountCredential.<RequestAccessTokenAsync>d__b.MoveNext()<---
我已确保在 packages.config 中有以下最新版本:
<package id="Microsoft.Bcl" version="1.1.6" targetFramework="net40" />
<package id="Microsoft.Bcl.Async" version="1.0.165" targetFramework="net40" />
<package id="Microsoft.Bcl.Build" version="1.0.13" targetFramework="net40" />
<package id="Microsoft.Net.Http" version="2.2.18" targetFramework="net40" />
以及 app.config 中的以下内容:
<dependentAssembly>
<assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.5.19.0" newVersion="2.5.19.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Threading.Tasks" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.5.19.0" newVersion="2.5.19.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.1.10.0" newVersion="2.1.10.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Net.Http.Primitives" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.1.10.0" newVersion="2.1.10.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Threading.Tasks.Extensions.Desktop" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.0.165.0" newVersion="1.0.165.0" />
</dependentAssembly>
所以,我的问题是为什么它在我的开发机器上运行良好,并在我们的 QA 环境(Window Server 2008 R2 标准)中抛出我上面提到的异常?