Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DLL hell - Could not load file or assembly System.Threading.Tasks.Extensions

I have very simple .NET Framework 4.7.2 app with the following nugets:

<packages>
  <package id="Autofac" version="5.1.2" targetFramework="net472" />
  <package id="DynamicData" version="6.14.8" targetFramework="net472" />
  <package id="Microsoft.Bcl.AsyncInterfaces" version="1.1.0" targetFramework="net472" />
  <package id="MSBuildTasks" version="1.5.0.235" targetFramework="net472" developmentDependency="true" />
  <package id="ReactiveUI" version="11.2.3" targetFramework="net472" />
  <package id="Serilog" version="2.9.0" targetFramework="net472" />
  <package id="Serilog.Enrichers.Process" version="2.0.1" targetFramework="net472" />
  <package id="Serilog.Enrichers.Thread" version="3.1.0" targetFramework="net472" />
  <package id="Serilog.Sinks.File" version="4.1.0" targetFramework="net472" />
  <package id="Splat" version="9.3.11" targetFramework="net472" />
  <package id="System.Reactive" version="4.3.2" targetFramework="net472" />
  <package id="System.Runtime.CompilerServices.Unsafe" version="4.7.0" targetFramework="net472" />
  <package id="System.Threading.Tasks.Extensions" version="4.5.3" targetFramework="net472" />
  <package id="System.ValueTuple" version="4.5.0" targetFramework="net472" />
</packages>

As you can see, there is System.Threading.Tasks.Extensions referenced in version 4.5.3 (I can't change this version, because other packages depend on it).

When I go to properties of this DLL reference in Visual Studio Solution Explorer, it shows version 4.2.0.1 referenced from .....\packages\System.Threading.Tasks.Extensions.4.5.3\lib\netstandard2.0\System.Threading.Tasks.Extensions.dll.

In app.config there is automatically generated binding redirect:

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

When I run the app, I get exception:

Exception message

I already tried to change binding redirect to something like:

   <bindingRedirect oldVersion="0.0.0.0-99.99.99.99" newVersion="4.5.3" />

But the issue persists. I have no idea how to resolve this dll-hell. Any help will be much appreciated.

like image 866
Signum Avatar asked Mar 12 '20 22:03

Signum


People also ask

What is the DLL hell issue?

The original DLL hell issue was this: Several applications use a shared DLL file. Then, one of the applications updated the DLL file and now the other applications no longer work. In .NET, this issue is solved. We will usually ship DLL files separately for each application.

Will the runtime load the correct version of the Assemblies?

Since both assemblies are installed in the GAC, the runtime will find and load the correct versions. The only caveat here is that unlike in the other solutions, where the output folder could be deployed as is, you’ll have to take care to install the assemblies in the GAC of the deployed computer.

What to do when an assembly fails to load?

Since the assemblies aren’t in the output folder (they are in sub-folders), the assemblies will fail to load. Let’s register to AssemblyResolve event, which fires when an assembly’s failed to load, like this: On each failed assembly load, we will parse the required version and manually load the assembly from the subfolders.

Do you ship DLL’s separately for each application?

We will usually ship DLL files separately for each application. Alternatively, we can use the GAC for shared DLL’s, which supports versioning. This means we can store different versions of the same DLL and the different applications will load their intended version. In the modern world, we are dependent on dozens of libraries.


1 Answers

When I go to properties of this DLL reference in Visual Studio Solution Explorer, it shows version 4.2.0.1 referenced from xxxxxx

First, I think you have a little misunderstanding about the DLL version and the nuget version.

version 4.5.3 is just the nuget package System.Threading.Tasks.Extensions's version number while version 4.2.0.1 is just the System.Threading.Tasks.Extensions.dll( exist in the nuget pacakge) number. They are not a concept at all. And you can see such dlls referenced in xxx.csproj file.

enter image description here

A version of the nuget package contains multiple competing DLLS for different project frameworks, which will be automatically installed into the corresponding project according to the nuget mechanism.

DLL hell - Could not load file or assembly System.Threading.Tasks.Extensions

As you said, you have a nuget package called System.Reactive version 4.3.2 that depends on System.Threading.Tasks.Extensions 4.5.3 in your project. You can see this:

enter image description here

And from your error log, it seems that the project referenced System.Threading.Tasks.Extensions.dll 4.2.0.0 which it does not exists in the nuget package System.Threading.Tasks.Extensions 4.5.3 and it should be System.Threading.Tasks.Extensions.dll 4.2.0.1. Not sure if you changed the version number or the reason for the project.

You can check these steps:

Solution

1) make sure that System.Threading.Tasks.Extensions version is 4.5.3 in packages.config file.

2) make sure that you change the dll version to 4.2.0.1 in xxx.csproj file

3) Before you reinstall these, please clean the nuget cache first, then run update-Package -reinstall under Tools-->Nuget Package Manager->Package Manager Console to reinstall the packages

4) create a new framework 4.7.2 project and then referenced these nuget packages to test whether it is caused by your project.

like image 180
Mr Qian Avatar answered Sep 19 '22 23:09

Mr Qian