c# - Microsoft.Threading.Tasks 引用了不正确的 System.Threading.Tasks.dll 版本

标签 c# .net visual-studio base-class-library

我正在使用 .NET Framework 4.0 开发 C# 库。

在这个库上,我安装了这些 NuGet 包:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Microsoft.Bcl" version="1.1.10" targetFramework="net40" />
  <package id="Microsoft.Bcl.Async" version="1.0.168" targetFramework="net40" />
  <package id="Microsoft.Bcl.Build" version="1.0.21" targetFramework="net40" />
  <package id="Microsoft.Net.Http" version="2.2.29" targetFramework="net40" />
  <package id="Newtonsoft.Json" version="6.0.8" targetFramework="net40" />
</packages>

这是我的 App.config 内容:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.2.29.0" newVersion="2.2.29.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.6.10.0" newVersion="2.6.10.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Threading.Tasks" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.6.10.0" newVersion="2.6.10.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

在此项目中,我引用了 System.Threading.Tasks 版本 2.6.10.0。我创建了一个安装程序并将该项目安装在另一台计算机上。我已在安装程序(在 GAC 上安装此 dll)上添加了 System.Threading.Tasks 版本 2.6.10.0。但是当我运行可执行文件时,我收到此错误:

Could not load file or assembly 'System.Threading.Tasks, Version=1.5.11.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
   at MyProject.Common.WebApi.Client.AsyncHelpers.<>c__DisplayClass7 1.<RunSync>b__6(Object _)
   at MyProject.Common.WebApi.Client.AsyncHelpers.ExclusiveSynchronizationContext.BeginMessageLoop()
   at MyProject.Common.WebApi.Client.AsyncHelpers.RunSync[T](Func 1 task)
   at MyProjectCodesManagerWindowsService.WebApi.Client.MyProjectCodesManagerClient.PrepareAndStartv2(String orderNumber, String userName, String systemName)

但在所有项目中,我都引用了 System.Threading.Tasks 版本 2.6.10.0

为什么我会收到此异常?有什么建议吗?

我已经检查了此页面 https://blogs.msdn.microsoft.com/bclteam/p/asynctargetingpackkb/ 中的所有问题但结果相同。

在本页之后,http://pauliom.com/2012/01/30/how-to-log-net-binding-errors-when-you-dont-have-fuslogvw-exe/ ,我得到了这个日志:

Calling assembly : Microsoft.Threading.Tasks, Version=1.0.12.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a.
===
LOG: Start binding of native image System.Threading.Tasks, Version=1.5.11.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a.
WRN: No matching native image found.

问题出在 Microsoft.Threading.Tasks,Version=1.0.12.0 上,它是 System.Threading.Tasks version=1.5.11.0 的依赖项。我在文件夹 D:\packages\Microsoft.Bcl.1.1.10\lib\sl4\ 上找到了该 dll。

使用程序 .NET Reflector 我发现 Microsoft.Threading.Tasks, Version=1.0.12.0System.Threading.Tasks version=1.5.11.0 有依赖关系>.

当我修改所有 App.config 文件以使用版本 2.6.10.0 时,为什么它使用该 dll 版本?

最佳答案

我找到了解决方案。

我必须添加这个:

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.2.29.0" newVersion="2.2.29.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.6.10.0" newVersion="2.6.10.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Threading.Tasks" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.6.10.0" newVersion="2.6.10.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

进入程序的App.Config,它将使用我的库。

问题是我必须将我的库中的 dependentAssembly 添加到程序的 app.config 中,它将使用这个库。

我在这里找到了解决方案:https://social.msdn.microsoft.com/Forums/en-US/f0653a7f-4196-4b5e-8d96-61d75fb8274e/gac-dll-and-appconfig?forum=clr

关于c# - Microsoft.Threading.Tasks 引用了不正确的 System.Threading.Tasks.dll 版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35431993/

相关文章:

c++ - 通过带有Visual Studio 2015的NuGet包的OpenCV,如何配置?

c# - WPF - 如何向标签添加效果(如阴影)

c# - 模糊文本匹配

c# - 使用StructureMap注入(inject)相应的实现

c# - 对简单类型使用结构而不是类

c# - 我可以替换 <see cref =""> 文档标签的值吗?

c# - 使用mysql调试c#代码的正确方法

c# - 我可以重构我的 linq select 吗?

c# - Windows 8.1 通用应用程序中的 BackgroundWorker 的替代方案是什么?

c# - Stored proc输出参数+Select不传回输出参数