79
votes

I've got a VS2017 project that compiles to a DLL which is then called by an EXE written by someone else. Both projects target .Net Framework 4.6.2. I rewrote one of my DLL methods to return a tuple and also imported the associated NuGet package. When I compile the project it includes System.ValueTuple.dll in the output directory which is then deployed to other machines where my DLL is loaded and called by the EXE. But when the EXE attempts to call the method that returns a tuple it crashes:

Unexpected Error Could not load file or assembly 'System.ValueTuple, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the file specified.

I'm not understanding why it's not finding the file since it's in the same folder as my DLL. Apparently MS did not include this assembly in .Net Framework 4.6.2.

Note that my DLL is registered in Windows using a machine.config file. I'm guessing that if I also add System.ValueTuple.dll to this file it will work (haven't tried yet and not sure this is the best approach, especially long term.) Is there a better way, besides waiting for 4.6.3 and hoping it includes this assembly?

19
"Note that my DLL is registered in Windows using a machine.config file" - do you mean your DLL is being put in the GAC, but System.ValueTuple.dll isn't? I can see how that would cause problems.Jon Skeet
@JonSkeet Yes. But I was under the assumption that Windows will look for referenced DLLs in the same folder as the calling application, which in this case is my DLL.Mike Lowery
The path of the DLL never plays a role to find assemblies, only the startup EXE.Hans Passant
ValueTuple is built into .NET Framework 4.7, which was just announced and will be released shortly. That said, I don't understand why you ran into a problem with 4.6.2, since you imported the ValueTuple package from nuget. I'm not an expert at assembly binding, but your solution (below) worries me. Feel free to file an issue on the rolsyn repo with a small repro project (zipped).Julien Couvreur
I ran into the same problem after updating (seemingly un-related) Nuget packages. Upgrading to 4.7.2 resolved the issue, but I also had to remove binding references.sbkrogers

19 Answers

59
votes

ok this feels completely wrong but I cut

  <dependentAssembly>
    <assemblyIdentity name="System.ValueTuple" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
  </dependentAssembly>

This out of my web.config for the main application.

I was really just seeing what happened to see if there was an underlying dependency or something, not expecting it to run. It just carried on working, all the new functions I have added in the last few days still work.

47
votes

I just had this issue myself. Not on Localhost while developing, but only on production server. In the end it turned out to be some sort of conflict between .Net Framework 4.6.1 and me having System.ValueTuple installed from Nuget in version 4.5.0.

The solution turned out to be, to downgrade the System.ValueTuple Nuget package to 4.3.0. Then it worked, like nothing had ever been an issue.

I suspect that this only happened on production server, cause of a different version of .net framework installed.

16
votes

Solved it by installing .NET Framework 4.7.2 Runtime on the machine the error occurred on. Simple and no need to add bindingRedirect or downgrading NuGet packages.

https://dotnet.microsoft.com/download/dotnet-framework/net472

11
votes

FWIW, I had this issue on my testing project using Moq. Someone had set the project to .NET 4.7, but I was on 4.6.2. Not wanting to move to 4.7 yet, the solution was to downgrade the version to Moq 4.7.145. The System.ValueTuple v 4.3.1 worked together with it.

8
votes

I resolved this issue by registering System.ValueTuple in my computer's machine.config file (along with my own DLL which was already registered there). I don't particularly like this approach though since it's dependent upon the DLL version which is subject to change at any time. Hopefully MS will just add this assembly to the next version of the .Net Framework.

<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
    <assemblyIdentity name="System.ValueTuple" publicKeyToken="cc7b13ffcd2ddd51" />
    <bindingRedirect oldVersion="0.0.0.0-99.99.99.99" newVersion="4.0.1.0" />
    <codeBase version="4.0.1.0" href="e:\exec\System.ValueTuple.dll" />
  </dependentAssembly>
  ...
</assemblyBinding>
</runtime>
7
votes

I faced the same exception when there was a nuget package version mismatch. (In the DLL was 4.3.1 used while in the main program 4.3.0.) I have resolved the issue by upgrading the packages to the same version... Checking and unifying the package versions could hopefully help you as well.

4
votes

Adding on to Robin's answer for just changing the Web.config. I was able to get away with only commenting out the binding redirect tag.

<dependentAssembly>
    <assemblyIdentity name="System.ValueTuple" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
    <!--<bindingRedirect oldVersion="0.0.0.0-4.0.2.0" newVersion="4.0.2.0" />-->
</dependentAssembly>

This got rid of the error for me.

1
votes

My issue was that I was developing against 4.6.1, but releasing on 4.7.2. Luckily I don't mind which .Net framework this project was built for, so I installed 4.7.2 on my developer instance, then upgraded all the Nuget packages.

(Using SQLite on AWS EC2)

1
votes

I hade the same issue, i solve the probleme by changing the target framwork of the project to .Net Framwork 4.7.1.

System.ValueTuple now supports .NET Framework 4.7

1
votes

If you have AutoMapper version 8.0 or lower referenced in any of your projects, it's possibly the source of the issue. See this github issue for more information.

If I understand correctly, the issue is that .NET Framework versions below version 4.7 did not have System.ValueTuple shipped with them by default, so AutoMapper was using a NuGet package reference for the assembly since it has build targets for Framework versions below 4.7. This caused some funky Microsoft stuff.

The easiest solution is to upgrade your AutoMapper references to version 8.1.0 or newer, where they have scraped all uses of the assembly from their codebase and removed the dependency.

1
votes

I hope this isn't thread necromancy, because this is still the #1 searched thing on Google. None of the other comments worked for me sadly.

We resolved this issue recently after over a year with this problem. The issue was a package called 'GitVersion'. So for anybody who is still struggling with this and looking around the forums I know quite a few are; I would advise you check your packages and see what their dependencies are.

0
votes

I had this same issue with an AutoMapper 8.0.0.0 dependency on version 4.5 after upgrading from .NET 4.5.1 to 4.6.1. Reinstalling the automapper nuget package worked for me.

0
votes

IF your are unable to update .Net Framework to the latest version, then downgrade Package: Microsoft.Net.Compilers to the version up to 2.10. This solved the issue in my case.

0
votes

I had a library which used a newer version of the System.ValueTuple NuGet package. I then used another library which caused me to use the older version installed in the main project for the first time. That caused this exception to reveal itself. Updating both (or, converging them in any way - downgrading both is fine too) - fixed the issue.

0
votes

In my solution I found 2 different trouble maker. Either in the App.config or Web.config file:

  1. Version mismatch: The version installed via NuGet did not match the version in the config file. Solution: Change the version manually in the .config file.

  2. Duplicate entries: I found duplicate entries for ValueTuple. In my case one for 4.0.3.0 and another one for 4.5.0. Removing the older entry solved the issue.

In another case I managed to fix the issue by removing unneeded references and getting rid of the ValueTuple NuGet package altogether.

0
votes

I resolved this issue by installing System.ValueTuple from nuget. It was not previously installed but I guess RestSharp or another library is using it.

So this got it fixed.

0
votes

In my case I think something delete this dll from project and framework folders, maybe during installing something it flied; so my project during debugging couldn't find that dll and throw that error. I installed

Install-Package System.ValueTuple -Version 4.5.0

Package and than everything worked again. Before doing further complicated solutions as described above answers may installing ValueTuple package works for you.

0
votes

I experienced the same error "Could not load file or assembly System.ValueTuple.dll..." on my Windows Server 2016. However, the site worked fine on my dev machine.

My solution was simple, I grabbed this dll from my dev machine and dropped it in the site's "bin" folder on the server. It worked.

0
votes

Solved it by installing VS 2019.