【问题标题】:Google.Apis.Http.ConfigurableMessageHandler exception at runtime运行时的 Google.Apis.Http.ConfigurableMessageHandler 异常
【发布时间】:2014-12-18 15:03:44
【问题描述】:

我对 Google Analytics API .NET 有疑问。 在线通过 api 连接到 GA 时

        var certificate = new X509Certificate2(pathToPrivateKey, passToPrivateKey, X509KeyStorageFlags.Exportable);
        ServiceAccountCredential credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(serviceAccountEmailAddress)
        {
            Scopes = scopes
        }.FromCertificate(certificate));

它给出以下错误

Message=The type initializer for 'Google.Apis.Http.ConfigurableMessageHandler' threw an exception.
  Source=Google.Apis.Core
  TypeName=Google.Apis.Http.ConfigurableMessageHandler
  StackTrace:
   at Google.Apis.Http.ConfigurableMessageHandler..ctor(HttpMessageHandler httpMessageHandler)
   at Google.Apis.Http.HttpClientFactory.CreateHttpClient(CreateHttpClientArgs args) in c:\code\google.com\google-api-dotnet-client\default\Tools\Google.Apis.Release\bin\Debug\test\default\Src\GoogleApis.Core\Apis\Http\HttpClientFactory.cs:line 37
   at Google.Apis.Auth.OAuth2.ServiceAccountCredential..ctor(Initializer initializer) in c:\code\google.com\google-api-dotnet-client\default\Tools\Google.Apis.Release\bin\Debug\test\default\Src\GoogleApis.Auth.DotNet4\OAuth2\ServiceAccountCredential.cs:line 217
   at ConsoleApplication3.Program.AuthenticationGA(String pathToPrivateKey, String serviceAccountEmailAddress, String passToPrivateKey) in C:\Documents and Settings\pivotaladmin.WA-PIV\my documents\visual studio 2010\Projects\ConsoleApplication3\ConsoleApplication3\Program.cs:line 30
   at ConsoleApplication3.Program.Main(String[] args) in C:\Documents and Settings\pivotaladmin.WA-PIV\my documents\visual studio 2010\Projects\ConsoleApplication3\ConsoleApplication3\Program.cs:line 69
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.IO.FileLoadException
   Message=Could not load file or assembly 'System, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e, Retargetable=Yes' or one of its dependencies. The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)
   Source=Google.Apis.Core
   FileName=System, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e, Retargetable=Yes
   FusionLog==== Pre-bind state information ===

项目类型:控制台应用
.NET 框架 4.0
MS Visual Studio 2010 速成版
视窗服务器 2003

我在 Windows 7 上尝试了相同的代码,它可以工作。
我不知道出了什么问题。
任何想法?

【问题讨论】:

  • 检查 GAC 中是否没有多余的 dll 浮动或将引用 dll 设置为复制本地。
  • 所有外部 dll - 复制本地:是的,我认为问题可能出在 C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0 我没有 System.Net .Http.dll 所以我使用命令 gacuitl /i System.Net.Http.dll 添加它。该 dll 由 HttpClientFactory.cs 使用。
  • @DalmTo 我检查了第二台机器 Windows Server 2003 R2,这次在运行时 VS 询问我:c:\code\google.com\google-api-dotnet-client\default\Tools\ Google.Apis.Release\bin\Debug\test\default\Src\GoogleApis.Core\Apis\Http\HttpClientFactory.cs
  • @DalmTo 在 Windows Server 2008 R2 上检查 - 效果很好。

标签: google-analytics-api windows-server-2003 google-api-dotnet-client system.net service-accounts


【解决方案1】:

看起来你的主要问题是你得到的内部异常:

InnerException: System.IO.FileLoadException
 Message=Could not load file or assembly 'System, Version=2.0.5.0, Culture=neutral,
 PublicKeyToken=7cec85d7bea7798e, Retargetable=Yes' or one of its dependencies. 

好消息是您应该只安装以下补丁:KB2468871,如下所示: Could not load file or assembly System, Version=2.0.5.0 in .NET 4 MVC 4 application

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-03
    • 2014-02-26
    • 2013-04-06
    • 2014-01-14
    • 2017-02-04
    • 2012-03-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多