43

I have a web project build on .net framework 4.5.1. We are trying to added PostgreSQL support for the project. Using Nuget, I have installed 4.0.4 npgsql to the project. Under references, I see the following being added to the project.

  1. Npgsql - 4.0.4.0 - Runtime version v4.0.30319
  2. System.Threading.Tasks.Extensions - 4.2.0.0 - Runtime version v4.0.30319

When I tried run the project and connect and get the data from the database, I am getting the following error saying 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.

System.Threading.Tasks.Extensions which is installed using Nuget is not getting loaded to the project. When I checked the properties of System.Threading.Tasks.Extensions reference, the dll file exists in the location. I have also tried installing System.Threading.Tasks.Extensions.dll file to assembly using gacutil. I am still getting the same error.

Please let me know if you need any additional information.

Any help is really appreciated.

404
  • 8,022
  • 2
  • 27
  • 47
User12111111
  • 1,179
  • 1
  • 19
  • 41
  • The exception comes from the static or type initializer. A part that should never throw exceptions, due to poor debugability. Note that it says the file itself - in 2 versions - or one of it's dependencies. So the chances are 90% it is one of the depedencies that both versions use. – Christopher Dec 03 '19 at 11:59
  • For reference, this is an open bug in `Npgsql` that is being discussed here: https://github.com/npgsql/npgsql/issues/2677. The cause of the issue and several workarounds are discussed in that thread – mark.monteiro Apr 07 '21 at 21:57

10 Answers10

56

In my case, I got the issue after upgrading to version 4.5.4 and tried @user2713341 answer. It didn't work but put me in the right direction.

My project had no bindings for this library, so I added the binding and it worked

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

and it worked.

Note that it should be 4.2.0.1 even though the version is 4.5.4.

Marcus
  • 443
  • 6
  • 6
Keyjote
  • 783
  • 1
  • 6
  • 12
  • 3
    I tried installing 4.5.4 explicitly and using '' but that didn't work for some reason. Your answer worked for me however, so thanks a bunch :) – RobJohnson Jul 05 '20 at 17:20
  • 1
    The Nuget package says version 4.5.4. The DLL itself says file version 4.6.28619.1 and your assembly binding redirect to 4.2.0.1 works for me after some SpecFlow tests did not accept to run. Makes no sense, but thanks for your advice - 4.2.0.1 binding redirect is the way to go. Tested in VS 2019 and Specflow 2019 tests. – Tore Aurstad Dec 16 '20 at 09:20
  • 3
    @ToreAurstad Actually, the version of the DLL in System.Threading.Tasks.Extensions v4.5.4 is 4.2.0.1. You can check the nupkg manually [here](https://www.nuget.org/packages/System.Threading.Tasks.Extensions/4.5.4). Seems that for some reason, Microsoft is shipping Nuget packages with inconsistent version numbers between the Nuget and DLL. Only god knows why. – scharnyw Jan 05 '21 at 07:32
  • Life saver! Thanks – Samy Sammour Dec 01 '21 at 08:24
  • Thanks! Apparently the Nuget version version is not the same as the assembly version. We can check the assembly version [here](https://nuget.info/packages/System.Threading.Tasks.Extensions/4.5.4) – Sahitya Feb 14 '22 at 08:16
  • In my case, some of the missing dlls didn't even have a `` entry. Adding them with the `` solved the issue. – Sven Möhring Feb 15 '23 at 14:34
23

Update Nuget Package

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

will solve your problem

Jitendra Morya
  • 244
  • 2
  • 2
10

Update Nuget Package is not working for me.

What works? In app.config need to change

<bindingRedirect oldVersion="0.0.0.0-4.2.0.1" newVersion="4.2.0.1" />

To

<bindingRedirect oldVersion="0.0.0.0-4.5.4" newVersion="4.5.4" />

for current version 4.7.2 should work.

Microsoft like ;)

user2713341
  • 124
  • 3
  • 8
  • I thought this was a daft answer, given that on my dev environment `oldVersion="0.0.0.0-4.2.0.1" newVersion="4.2.0.1"` was the correct solution... BUT THEN! ... it didnt work on my staging environment, so I tried `oldVersion="0.0.0.0-4.5.4" newVersion="4.5.4"` and it worked... but how? I am too thick to understand obvsiouly :D – Jimbo Mar 01 '21 at 13:17
  • 3
    4.5.4 is the _package_ version. 4.2.0.1 is the _assembly_ version. Binding redirects work only on the _assembly_ version. – Frank Shearar Mar 03 '21 at 15:26
  • @FrankShearar - thats why I thought it was a daft answer, but it turns out that this answer is correct. `` is the solution (and I have now used that in the production environments of four different applications) – Jimbo Apr 28 '21 at 13:03
2

The response from @Keyjote was at the root of the solution for me, but rather than cherry-picking the assemblies, I was able to just reinstall. This seemed to automatically repair the app.config file.

Tools -> Nuget Packet Manager -> Packet Manager Console

Update-Package -reinstall -Project <your project name>

This way you don't need to mess with the syntax or have to figure out the PublicKeyToken values.

If you want to do it for the whole solution, you can omit the -Project <> parameter.

Hambone
  • 15,600
  • 8
  • 46
  • 69
2

I got the error in a different context when trying to migrate using Entity Framework Core (EFC) Version 3.1.8 using the Package Manager Console. The project built successfully.

Trying the binding redirects as suggested in this thread and (re)installing different versions of System.Threading.Tasks.Extensions as well as EFC (as suggested also elsewhere) did not work for me.

I managed to solve the problem by

  • deleting the packages/ directory in my project's root directory
  • using manual NuGet Package Restore (link to Microsoft Docs) via Visual Studio
Daniel
  • 76
  • 4
1

problem lies in *.csproj file. having wrong reference for System.Threading.Tasks.Extensions.4.5.4 because this dll not loading. after referring correct framework version folder from lib its start working old Reference:

<Reference Include="System.Threading.Tasks.Extensions, Version=4.2.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
      <HintPath>..\packages\System.Threading.Tasks.Extensions.4.5.2\lib\netstandard2.0\System.Threading.Tasks.Extensions.dll</HintPath>
    </Reference>
corrected one:

<Reference Include="System.Threading.Tasks.Extensions, Version=4.2.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
      <HintPath>..\packages\System.Threading.Tasks.Extensions.4.5.4\lib\net472\System.Threading.Tasks.Extensions.dll</HintPath>
    </Reference>

please clean the nuget cache first, then run test case it will work

0

I have been struggling a lot with bindingRedirect. I finally found a real gotcha that solved my problems.

I have a WCF server project in .net 4.8, depending on projects in .net standard 2.0.

I was updating Nuget packages in the referred projects. I got al sorts of problems, I won't try to mention them here.

I had bindingRedirects in web.config. I had to rediscover that those were unnecessary, and maybe even conflicting, because of automatic generation of those into the dll.config. There are various aspects to make that work, see elsewhere.

But the real gotcha for me was that for IIS, I had to LINK web.config to the dll.config. The latter being the complete config-file, with all the bindingRedirects, which turned out to be working after all.

Robert
  • 53
  • 6
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/31700893) – Dan May 10 '22 at 19:12
0

This error happened to me when I tried to run a test using Resharper. Turned out I've chosen a wrong configuration file (from another project) for a test runner in Resharper test runner settings: enter image description here

Denis Koreyba
  • 3,144
  • 1
  • 31
  • 49
-1

Try to download the package and add reference to your project explicitly . should work , I just resolved it .

Limbo
  • 3
  • 2
  • Although this is a possible solution, it is not sustainable when working with many nuget packages and all their dependencies. For the long term, the best solution is still using the Nuget Package system, not avoiding it. – sɐunıɔןɐqɐp Jul 31 '20 at 11:01
-1

I got a similar error message - but for a different reason. In packages.config set by NUGET manager There was a ref for the new version - but in project reference there was a ref for an old version. The solution - delete the ref in project reference

Tal Stern
  • 9
  • 1