ASP.net应用程序崩溃 - 无法加载文件或程序集'Microsoft.Threading.Tasks.Extensions.Desktop'。

18

我想使用OAuth2和.Net 4.5框架来构建一个Google BigQuery的C# ASP.net应用程序。我安装了以下这些NuGet程序包

Install-Package Google.Apis.Bigquery.v2 -Pre
Install-Package Google.Apis.Authentication.OAuth2 -Version 1.2.4696.27634

Install-Package Google.Apis -Pre
Install-Package Google.Apis.Auth -Pre

我把相关的“usings”放在了后端文件“default.aspx.cs”中:

using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Bigquery.v2;
using Google.Apis.Bigquery.v2.Data;

namespace BigQueryDemoApp
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            UserCredential credential;
            FileStream stream;

            using (stream = new FileStream(
                    Server.MapPath("~/client_secrets.json"),
                    FileMode.Open, FileAccess.Read)
                )
            {
                GoogleWebAuthorizationBroker.Folder =
                    "Tasks.Auth.Store";
                credential = GoogleWebAuthorizationBroker.
                    AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    new[] { BigqueryService.Scope.Bigquery },
                    "user", CancellationToken.None).Result;
            }

            // Initialize the service.
            var Service = new BigqueryService(
                new BaseClientService.Initializer()
                {
                    HttpClientInitializer = credential,
                    ApplicationName = "BigQueryDemo"
                }
            );
        }
    }
}

我将这个特定的页面设置为项目的起始页面。在谷歌控制台构建客户端ID文件时,我选择了“已安装应用程序”。

APIS & auth -> Credentials -> CREATE NEW CLIENT ID

我确保使用VS2013中的解决方案资源管理器添加了这个文件(client_secrets.json)。在代码后台,我确保使用Server.MapPath正确地映射到client_secrets文件。对于凭据机制,我使用了以下代码

<https://code.google.com/p/google-api-dotnet-client/wiki/OAuth2>
作为起点。当我运行应用程序时,它返回一个浏览器错误页面,该页面以以下内容开头:

无法加载文件或程序集“Microsoft.Threading.Tasks.Extensions.Desktop,Version=1.0.16.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a”或其某个依赖项。系统找不到指定的文件。

并在“credential =”行崩溃。我尝试添加一些实际的ASP.net崩溃浏览器页面的图像显示程序集加载跟踪/堆栈跟踪/等等,但貌似我没有此权限。当我在“credential =”行处设置断点,然后通过以下方式运行应用程序时:

DEBUG -> Start Debugging
在VS2013中,“credential =”行停止运行,然后文件选择器打开,查找文件。
"GoogleClientSecrets.cs"

从目录中查找

"c:\code\google.com\google-api-dotnet-client\default\Tools\Google.Apis.Release\bin\Debug\output\default\Src\GoogleApis.Auth\OAuth2\GoogleClientSecrets.cs"

但是在磁盘上没有找到该文件。在生成的 ASP.NET 错误页面中使用程序集加载跟踪,我尝试浏览建议的配置文件,但都无法解决问题。总的来说,我也在 StackOverflow 上搜索了这个问题,虽然有一些相关的内容,但都没有帮助。

4个回答

29
由于错误基于最新版本的Microsoft.Bcl.Async在.NET 4.5中无法工作,您可以尝试执行以下操作:
打开Package Manager Console,并运行以下命令:
1)Uninstall-Package Microsoft.Bcl.Async -Force
2)Install-Package Microsoft.Bcl.Async -Version 1.0.16 它在我当前编写的示例中有效。如果对您有用,请告诉我。
更新(3月21日): 您可以更新软件包(新版本1.0.166-beta可用-https://www.nuget.org/packages/Microsoft.Bcl.Async/1.0.166-beta)。
我在VS2013上进行了测试,使用.NET 4.5框架,它可以运行。

你好。抱歉耽搁了。我尝试了你的建议,它有效。感谢你的帮助!
  • Frank
- Frank Solomon

4
他们发布了一个新版本的 Microsoft.Bcl.Async 包。
如果有人遇到此问题,请安装“最新”的版本,而不是 1.0.16 版本。
希望这对你有用。

非常清楚:安装Google API NuGet包,然后添加当前的Microsoft.Bcl.Async,它将替换Google包引用的内容。 - Guillaume

3
我之前遇到过这个错误。看起来 Bcl.Async 包在运行 .NET 4.0 应用程序时包含对 Microsoft.Threading.Tasks.Extensions.Desktop 的引用,但是在 .NET 4.5 应用程序中却缺少此功能。
我的建议是,在我弄清楚 Microsoft.Bcl.Async 所有者为什么会发生这种情况之前,将 packages\Microsoft.Bcl.Async.1.0.165\lib\net40\Microsoft.Threading.Tasks.Extensions.Desktop.dll 中的 Microsoft.Threading.Tasks.Extensions.Desktop 复制到您的 BIN 文件夹中。它应该可以解决这个问题。
更新(3月17日): 考虑将以下后期构建事件添加到您的项目中:
copy /Y "$(SolutionDir)packages\Microsoft.Bcl.Async.1.0.16\lib\net40\Microsoft.Threading.Tasks.Extensions.Desktop.dll" "$(TargetDir)Microsoft.Threading.Tasks.Extensions.Desktop.dll"
不幸的是,Bcl.Async 包的所有者还没有解决此问题的解决方案。

2

这种方法没有解决问题 - 我得到了相同的运行时错误。但是在重新构建后,我注意到VS2013编译器显示了这个警告,我对其进行了一些格式化,以便在SO编辑器中使用。

C:\Program Files (x86)\MSBuild\12.0\bin\Microsoft.Common.CurrentVersion.targets(1635,5): warning
MSB3247: Found conflicts between different versions of the same dependent assembly. In Visual 
Studio, double-click this warning (or select it and press Enter) to fix the conflicts; 
otherwise, add the following binding redirects to the "runtime" node in the application 
configuration file:

<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
        <assemblyIdentity name="Microsoft.Threading.Tasks.Extensions.Desktop" culture="neutral" publicKeyToken="b03f5f7f11d50a3a" />
        <bindingRedirect oldVersion="0.0.0.0-1.0.165.0" newVersion="1.0.165.0" />
    </dependentAssembly>
</assemblyBinding>

所以我将建议的代码块放入应用程序的web.config文件中。然后应用程序就决定工作了。我不知道为什么现在它能够工作,但我有印象XML块和/或您提到的参考修复某种程度上触及了Microsoft.Threading.Tasks.Extensions.Desktop DLL,或.NET内部的一些低级机制,或两者都有。或者说根本没有,我也不知道。无论如何,感谢您的帮助。我只希望我对内部机制有更好的理解。


我联系了这个NuGet包的所有者,希望我们很快能找到更好的解决方案。 - peleyal
显然,1.0.166版(https://www.nuget.org/packages/Microsoft.Bcl.Async/1.0.166-beta)修复了该问题。 - peleyal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接