无法加载 System.Threading.Tasks.Extensions

Use*_*111 26 c# npgsql nuget

我有一个基于 .net 框架 4.5.1 的 Web 项目。我们正在尝试为该项目添加 PostgreSQL 支持。使用 Nuget,我已经为项目安装了 4.0.4 npgsql。在参考下,我看到以下内容被添加到项目中。

  1. Npgsql - 4.0.4.0 - 运行时版本 v4.0.30319
  2. System.Threading.Tasks.Extensions - 4.2.0.0 - 运行时版本 v4.0.30319

当我尝试运行项目并连接并从数据库中获取数据时,出现以下错误,提示 FileNotFoundException:

    System.TypeInitializationException
      HResult=0x80131534
      Message=The type initializer for 'com.rsol.RConfig' threw an exception.
      Source=RConfig
      StackTrace:
       at com.rsol.RConfig.getInstance() in C:\Workspaces\PS\RConfig\RConfig.cs:line 1113
       at RAdmin.Global.Application_Start(Object sender, EventArgs e) in C:\Workspaces\PS\RAdmin\Global.asax.cs:line 528

    Inner Exception 1:
    TypeInitializationException: The type initializer for 'com.rsol.Db.DbMgr' threw an exception.

    Inner Exception 2:
    FileNotFoundException: Could not load file or assembly 'System.Threading.Tasks.Extensions, Version=4.2.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the file specified.

    Inner Exception 3:
    FileNotFoundException: Could not load file or assembly 'System.Threading.Tasks.Extensions, Version=4.2.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the file specified.
Run Code Online (Sandbox Code Playgroud)

使用 Nuget 安装的 System.Threading.Tasks.Extensions 未加载到项目中。当我检查 System.Threading.Tasks.Extensions 引用的属性时,dll 文件存在于该位置。我还尝试使用 gacutil 将 System.Threading.Tasks.Extensions.dll 文件安装到程序集。我仍然遇到同样的错误。

如果您需要任何其他信息,请告诉我。

任何帮助都非常感谢。

小智 27

就我而言,我在升级到 4.5.4 版后遇到了这个问题,并尝试了 @user2713341 的答案。它没有用,但让我朝着正确的方向前进。

我的项目没有绑定这个库,所以我添加了绑定并且它起作用了

<dependentAssembly>
  <assemblyIdentity name="System.Threading.Tasks.Extensions" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
  <bindingRedirect oldVersion="0.0.0.0-4.2.0.1" newVersion="4.2.0.1" />
</dependentAssembly>
Run Code Online (Sandbox Code Playgroud)

它奏效了。

请注意,即使版本是 4.5.4,它也应该是 4.2.0.1。

  • 我尝试显式安装 4.5.4 并使用 '&lt;bindingRedirect oldVersion="0.0.0.0-4.5.4" newVersion="4.5.4" /&gt;' 但由于某种原因这不起作用。不过,你的回答对我有用,所以非常感谢:) (3认同)
  • @ToreAurstad实际上,System.Threading.Tasks.Extensions v4.5.4中DLL的版本是4.2.0.1。您可以[此处](https://www.nuget.org/packages/System.Threading.Tasks.Extensions/4.5.4)手动检查nupkg。似乎由于某种原因,微软正在运送 Nuget 包,其 Nuget 和 DLL 之间的版本号不一致。只有上帝知道为什么。 (3认同)

小智 15

更新 Nuget 包

https://www.nuget.org/packages/System.Threading.Tasks.Extensions/

会解决你的问题

  • 就我而言,此“更新”解决了问题,但我不得不将其降级。 (2认同)
  • 就我而言,Npgsql 特别要求版本 4.2.00。4.5.3 版本是问题所在。 (2认同)

小智 8

更新 Nuget 包对我不起作用。

什么工作?在 app.config 需要更改

<bindingRedirect oldVersion="0.0.0.0-4.2.0.1" newVersion="4.2.0.1" />
Run Code Online (Sandbox Code Playgroud)

<bindingRedirect oldVersion="0.0.0.0-4.5.4" newVersion="4.5.4" />
Run Code Online (Sandbox Code Playgroud)

对于当前版本 4.7.2 应该可以工作。

微软喜欢;)

  • 4.5.4 是_package_ 版本。4.2.0.1 是_程序集_版本。绑定重定向仅适用于 _ assembly_ 版本。 (3认同)