Microsoft.Threading.Tasks referencing an incorrect System.Threading.Tasks.dll version

懵懂的女人 提交于 2019-12-07 22:26:02

问题


I'm developing a C# library with .NET framework 4.0.

On this library I have these NuGet packages installed:

<?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>

This is my App.config content:

<?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>

On this project I'm referencing System.Threading.Tasks version 2.6.10.0. I have created an installer and install this project in another computer. I have added System.Threading.Tasks version 2.6.10.0 on the installer (that install this dll on GAC). But when I run the executable I get this error:

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)

But on all of the projects I'm referencing System.Threading.Tasks version 2.6.10.0.

Why I'm getting this exception? Any advice?

I have checked all the issues from this page https://blogs.msdn.microsoft.com/bclteam/p/asynctargetingpackkb/ but with the same result.

Following this page, http://pauliom.com/2012/01/30/how-to-log-net-binding-errors-when-you-dont-have-fuslogvw-exe/, I got this log:

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.

The problem is with Microsoft.Threading.Tasks, Version=1.0.12.0 it is a dependency with System.Threading.Tasks version=1.5.11.0. I have found that dll on folder D:\packages\Microsoft.Bcl.1.1.10\lib\sl4\.

Using program .NET Reflector I have seen that Microsoft.Threading.Tasks, Version=1.0.12.0 has a dependency with System.Threading.Tasks version=1.5.11.0.

Why is it using that dll version when I have modified all App.config files to use version 2.6.10.0?


回答1:


I have found the solution.

I have to add this:

<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>

into the program's App.Config that it is going to use my library.

The problem was that I have to add dependentAssembly from my library into the program's app.config that it's going to use this library.

I have found the solution here: https://social.msdn.microsoft.com/Forums/en-US/f0653a7f-4196-4b5e-8d96-61d75fb8274e/gac-dll-and-appconfig?forum=clr



来源:https://stackoverflow.com/questions/35431993/microsoft-threading-tasks-referencing-an-incorrect-system-threading-tasks-dll-ve

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!