Could not load file or assembly 'System.Threading.Tasks.Extensions, Version=4.2.0.0
Asked Answered
B

11

25

I recently installed CsvHelper (https://joshclose.github.io/CsvHelper/) when i try to use the library I get the following error:

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 located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

In my app.config I have 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>

Also in my project I have reference to

\packages\System.Threading.Tasks.Extensions.4.5.3\lib\netstandard2.0\System.Threading.Tasks.Extensions.dll

Its version is 4.2.0.1 I am not sure why its still trying to load 4.2.0.0 of the library.

my project is running .net 4.7.2

Bantling answered 12/2, 2020 at 17:53 Comment(4)
Is this a single project solution, or multi-project? I've noticed in Framework (not Core) that sometimes I need to install all packages and their dependencies into the entry-point project if they are used anywhere in the solution, or I get runtime errors.Minion
I have 2 projects a console app and the other is test project with nunit. The tests is referencing the console one both are .net 4.7.2 both have references to System.Threading.Tasks.ExtensionsBantling
Just created new console app .net core 2.2 and that works fine.Bantling
I needed to upgrade a different nuget dependency that was requiring an earlier version than what my projects had installed.Lavellelaven
C
21

I had the same problem today in a multi project solution, System.Threading.Tasks.Extensions got installed by embedding the Autofac package. After removing and reinstalling Autofac (via nuget) in both projects, the two packages.config contained the same entry

<package id="System.Threading.Tasks.Extensions" version="4.5.4" targetFramework="net472" />

but while one project referenced the correct dll in

packages\System.Threading.Tasks.Extensions.4.5.4\lib

the other one referenced the older version in

packages\System.Threading.Tasks.Extensions.4.5.2\lib

After manually removing the older dll from the references (not via nuget) and embedding the correct one, everything worked again. I don't see why the correct packages.config entry did not enforce the correct reference.

EDIT:

If it works on your dev machine but not on the machine you deployed to, don't forget to deploy the binding redirects in web.config / app.config. In my case they look like so:

<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>
Corettacorette answered 8/5, 2020 at 22:19 Comment(5)
What did you mean by "embedding"?Steffen
@StayOnTarget: embedding = installing via Nuget.Corettacorette
You saved my week !! :)Cutlass
Adding the binding redirect was the solution for me :) Thank you :)Macaw
i also had referenced Autofac, but, for me, the solution was to remove the above binding redirect from my app.configKoestler
M
9

I had this problem in a multi-project solution when upgrading .net version. In my case running Update-Package -reinstall command in Package Manager Console solved the problem.

Millennium answered 8/10, 2021 at 11:38 Comment(1)
I found useful the flag -ProjectName to target only one of my projects instead of the entire solutionHelbonna
S
4

According to https://github.com/JoshClose/CsvHelper/issues/1462 this seems to be a bug in one of the CSVHelper 15.0.0 dependencies. The 15.0.0 version (not sure about earlier ones) brings in some other assembly which causes the problem.

Personally I worked around this by downgrading to 13.0 (a comment also noted that 12.3.2 worked), but that bug report now states that they have released 15.0.1 specifically to fix this. I've yet to confirm that myself.

Steffen answered 6/3, 2020 at 22:23 Comment(1)
Installing CSVHelper v19.0.0 fixed this problem for me. Many thanks, would have been a real pain to diagnose.Cage
B
3

So i created a new .NET 472 project then added all the libs etc and code and now it works.

I suspect it originally broke because I had installed a really old version of csvhelper lib with excel plugin. Even though i removed it and removed all references it was still picking it up from somewhere.

Bantling answered 13/2, 2020 at 7:46 Comment(0)
E
2
<assemblyBinding>
  <probing privatePath="System.Threading.Tasks.Extensions.dll"/>
</assemblyBinding>
            <assemblyBinding>
  <probing privatePath="System.Runtime.CompilerServices.Unsafe.dll"/>
</assemblyBinding>
    <assemblyBinding>
  <probing privatePath="Microsoft.Bcl.AsyncInterfaces.dll"/>
</assemblyBinding>
Etheridge answered 14/9, 2020 at 20:24 Comment(2)
You need to reference the dll files then all will work at runtimeEtheridge
privatePath is bin\ ? (my app is web NET Framework 4.8)Rah
W
1

I had this problem today in a multi-project solution. Fixed it by updating other NuGet packages in the offending project. Unfortunately, not sure which one fixed the problem.

Current version of CsvHelper: 15.0.5.

Worthwhile answered 13/5, 2020 at 20:0 Comment(0)
J
1

In my case I had a different version of System.Threading.Tasks.Extensions in my unit testing project than in my main project so after I upgraded my unit testing project to match the version it worked for me.

Jalopy answered 22/12, 2022 at 18:45 Comment(1)
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewLukas
T
1

I go this error when running a console app (.NEt framework 4.7.2) consuming a "native" class library, which in turn consumed the System.Threading.Tasks.Extensions dll. Removing the class library from the console application and then re-adding it again, solved the issue.

Tonneau answered 1/9, 2023 at 6:53 Comment(0)
A
0

This answer assumes that you are using PackageReference for your nuget references. If that's the case, there is also a known bug in PackageReference .Net tooling model. For some weird cases, the nuget restore msbuild tool chain places ref assemblies, when it should have ideally placed the lib assemblies. How can you find out if you are facing a similar problem? You can use a standard reflector application like ILSpy to read the failing library's metadata in your working directory. If you see RefAssemblies attribute in its metadata, then you are also encountering this problem.

To fix this problem, you can use something like the following tag in your csproj file to fix the nuget copying issue.

<Target Name="ReplaceRefWithLib" BeforeTargets="ResolveAssemblyReferences">
    <ItemGroup>
        <_noCopyRefs Include="@(Reference)" Condition="'%(Reference.Private)' == 'false'" />
        <_noCopyRefsByFileName Include="@(_noCopyRefs->'%(FileName)')">
            <OriginalItem>%(Identity)</OriginalItem>
        </_noCopyRefsByFileName>
        <_libByFileName Include="@(ReferenceCopyLocalPaths->'%(FileName)')">
            <OriginalItem>%(Identity)</OriginalItem>
        </_libByFileName>
        <_overlappingRefByFileName Include="@(_noCopyRefsByFileName)" Condition="'@(_noCopyRefsByFileName)' == '@(_libByFileName)' AND '%(Identity)' != ''" />
        <_overlappingLibByFileName Include="@(_libByFileName)" Condition="'@(_noCopyRefsByFileName)' == '@(_libByFileName)' AND '%(Identity)' != ''" />
        <_overlappingRef Include="@(_overlappingRefByFileName->'%(OriginalItem)')" />
        <_overlappingLib Include="@(_overlappingLibByFileName->'%(OriginalItem)')" />
    </ItemGroup>
    <ItemGroup Condition="'@(_overlappingRef)' != ''">
        <Reference Remove="@(_overlappingRef)" />
        <Reference Include="@(_overlappingLib)">
        <Private>false</Private>
        </Reference>
    </ItemGroup>
</Target>

Hoping this helps.

Antwanantwerp answered 13/4, 2022 at 6:35 Comment(0)
M
0

I had similar issue for couple of dlls which added newly in C# project references where I was using .exe.config file to do binding redirect. I have mentioned old and new version in .exe.config file but it was giving same error. I used this link to enable binding redirect logs: How to enable assembly bind failure logging (Fusion) in .NET I found out that sometime dll getting loaded from wrong folder(as at multiple location same dll with different version are available) or wrong binding version given in .exe.config

example: Folder path where .exe.config placed : C:\folder1

Folder path where System.Threading.Tasks.Extensions.dll placed : C:\folder1\abc\xyz

enter image description here

Majordomo answered 22/2 at 15:16 Comment(1)
If you can, better to add code as text instead of as an image.Steffen
V
0

For those who tried but without result. Try removing the package and adding via NuGet again, for me it helped.

Vig answered 27/5 at 18:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.