Visual Studio 2017 - Could not load file or assembly 'System.Runtime, Version=4.1.0.0' or one of its dependencies
Asked Answered
P

28

141

I am using Visual Studio 2017 and am trying to create a .Net Standard 1.5 library and use it in a .Net 4.6.2 nUnit test project.

I am getting the following error...

Could not load file or assembly 'System.Runtime, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.

I have tried the following:

  1. Reference Std library as project reference. Error: gives me the previous error.
  2. Create a NuGet pkg for my Std library and reference that. Error: The type is System.String, expecting System.String. This is because System.Runtime ended up getting referenced by the project and it has definitions for all the standard types.
  3. Reference NuGet pkg NetStandard.Library. Error: give me the same error as # ("The type is System.String, expecting System.String"). NOTE: Before I did this, I cleared ALL NuGet packages from the project and then added just the nUnit and NetStandard.Library packages (which installed 45 other packages).

Is this a bug? Is there a work-around? Any help is appreciated.

Pasto answered 13/3, 2017 at 1:22 Comment(0)
I
124

I had the same problem and no suggested solutions that I found worked. My solution for this issue was: Check App.config and packages.config to see if the versions match.

Originally my app.config contained:

<dependentAssembly>
  <assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
  <bindingRedirect oldVersion="0.0.0.0-4.1.1.0" newVersion="4.1.1.0" />
</dependentAssembly>

But the packages.config contained:

<package id="System.Runtime" version="4.3.0" targetFramework="net461" requireReinstallation="true" />

I modified the app.config entry to match packages.config for the newVersion:

<dependentAssembly>
  <assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
  <bindingRedirect oldVersion="0.0.0.0-4.1.1.0" newVersion="4.3.0" />
</dependentAssembly>

After the change, the issue was resolved.

Irrupt answered 2/3, 2018 at 23:28 Comment(6)
Or just add the reference to your web.config: https://mcmap.net/q/116876/-build-error-you-must-add-a-reference-to-system-runtimeSholeen
I pulled "4.3.0" from NuGet but for some reason VS insists that I reference "4.1.2.0", similar work around just a different version number worked for me...Abercromby
I had the same problem like @DavidRogers in a MSTest project. Consolidating the differences between app.config and packages.config resolved the problem.Crump
yep thanks a bunch! This was the solution for my MSTest not finding tests [MSTest][Discovery] Failed to discover tests from assembly Reason:Could not load file or assembly 'System.Reflection, Version=4.1.1.0 etcTitan
Solution worked for me. Problem started after installing HtmlAgilityPack NUGET. And would not run because of incorrect version info in packages. +1Tatianna
Same issue. I just commented out the System.Runtime part in the App.Config file.Scumble
M
44

I encounter this issue recently and I tried many things mentioned in this thread and others. I added package reference for "System.Runtime" by nuget package manager, fixed the binding redicts in app.config, and make sure that app.config and package.config have the same version for the assembly. However, the problem persisted.

Finally, I removed the <dependentAssembly> tag for the assembly and the problem dissappeared. So, try removing the following in your app.config.

<dependentAssembly>
    <assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.1.0.0" newVersion="4.1.1.0" />
</dependentAssembly>

Edit: After I update .NET framework to 4.7.2, the problem resurfaced. I tried the above trick but it didn't work. After wasting many hours, I realized the problem is occurring because of an old System.Linq reference in app.config. Therefore, either remove or update all Linq references also to get rid of this problem.

Mckinney answered 10/9, 2018 at 1:59 Comment(3)
Whenever I run into the issue specified by the OP, I delete the System.Runtime information in the .config file and this resolves it. I am in agreement with you that this is a potential valid solution. It tends to happen with me when I add a package from nuget.Yalu
Worked for me. I have got an error xunit System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=4.1.2.0 after upgrading my projects to 4.7.2Lynettalynette
Based on your answer I checked my nuget packages and found 'Google.protobuf' needs (Consolidating) between my projects, thnxTrinitrocresol
T
39

This issue happens when you reference a .NET Standard project from a .NET 4.x project: none of the .NET Standard project's nuget package references are brought in as dependencies.

To fix this, you need to ensure your .NET 4.x csproj file is pointing to current build tools (at least 14):

<Project ToolsVersion="15.0">...

The below should no longer be needed, it was fixed around VS 15.3:

There was a known bug in VS2017, specifically in NuGet 4.0.

To work around the bug, you'll need to open up the .csproj file for your .NET 4.x project and add this snippet:

<ItemGroup>
  <PackageReference Include="Legacy2CPSWorkaround" Version="1.0.0">
    <PrivateAssets>All</PrivateAssets>
  </PackageReference>
</ItemGroup>

NuGet 4.x brings with it the "package reference" -- no more packages.config -- but the old 4.x pipeline was not fully updated at the time of VS2017's launch. The above snippet seems to "wake up" the build system to correctly include package references from dependencies.

Tierza answered 13/3, 2017 at 1:27 Comment(7)
Which update of Visual Studio 17? Can you specify the version?Elseelset
I still have the problem in 15.5.5 VS2017. It looks like there are other causes.Fenella
Question: is your .NET 4.x project using package references, or is it still using packages.config still? I'm wondering if the reason this appears fixed for me is that I've got rid of packages.config.Tierza
It's worth noting that Visual Studio 2017 Version 15.7 and later supports migrating a project from the packages.config management format to the PackageReference format. learn.microsoft.com/en-us/nuget/reference/…Octavalent
@tranquiltarn from your link: "Migration is not currently available for C++ and ASP.NET projects."Fergus
VS 15.9 here and this worked for me. The assembly is on a .Net Standard class library referenced by a .net 4.6.1 console app projectInextirpable
Crucial: after updating the toolchain, you need to regenerate the assembly bindings. This will remove the System.Runtime reference from them.Hakeem
C
33

Trust me, I am not joking. Remove all the System.Runtime dependencies from your app.config and it will start working.

Cosgrave answered 5/4, 2018 at 14:23 Comment(2)
A better explanation of why this would work would be helpful.Werby
The problem with this method is, whenever you update any nuget package or add new nuget package, it'll get added again.Hypo
C
17

I resolved that error by referencing the NetStandard.Library and the following app.config File in the NUnit-Project.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
            <assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-4.1.1.0" newVersion="4.1.1.0" />
        </dependentAssembly>
        <dependentAssembly>
            <assemblyIdentity name="System.Reflection" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-4.1.1.0" newVersion="4.1.1.0" />
        </dependentAssembly>
        <dependentAssembly>
            <assemblyIdentity name="System.Runtime.InteropServices" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-4.1.0.0" newVersion="4.1.1.0" />
        </dependentAssembly>
    </assemblyBinding>
</runtime>

Edit

If anything other than System.Runtime, System.Reflection or System.Runtime.InteropServices is missing (e.g. System.Linq), then just add a new dependentAssembly node.

Edit 2

In new Visual Studio Versions (2017 15.8 I think) it's possible that Studio creates the app.config File. Just check the Auto-generate binding redirects Checkbox in Project-Properties - Application. Auto-generate binding redirects

Edit 3

Auto-generate binding redirects does not work well with .NET Classlibraries. Adding the following lines to the csproj files solves this and a working .config file for the Classlibary will be generated.

<PropertyGroup>
  <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
  <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>
Cutinize answered 16/6, 2017 at 13:16 Comment(2)
Weirdly, I fixed the problem for me by removing all <dependentAssembly> nodes for System.Runtime..Abnegate
@MattBrewerton confirmed!Relativity
S
14

I fixed it by deleting my app.config with

<assemblyIdentity name="System.Runtime" ....> 

entries.

app.config was automatically added (but not needed) during refactoring

Sinner answered 24/4, 2018 at 13:15 Comment(1)
This worked for me! Definitely try this if all other items are not working out for youHectare
A
5

Into app.config or web.config add

<dependentAssembly>
    <assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
    <bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="4.0.0.0"/>
</dependentAssembly>
Atlee answered 24/6, 2019 at 10:40 Comment(1)
This has worked for me . Problem started after adding a .NET Core (then changed to .NET Standard) project. Problem had remained even after removing reference and resetting web.config (and trying most of the other answers that mentioned .NET standard).Cabalistic
C
4

This issue has many causes... in my case the problem was that in my web.config a tag adding the System.Runtime assembly:

<assemblies>
    <add assembly="System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</assemblies>

but one package also added the same assembly as dependency with other version:

<package id="System.Runtime" version="4.3.0" targetFramework="net47" />

removing the <add assembly> tag from my web.config resolved the issue.

Cheadle answered 9/11, 2018 at 14:17 Comment(0)
H
4

Seems like the issue is caused when there is version conflict between packages.config and app.config. In app.config you have assembly binding redirects automatically generated by thing called "AutoGenerateBindingRedirects". When enabled each time you download nuget package it will, additionaly to making new entry in packages.config, add this binding redirect information to app.config, what's the purpose of this is explained here: Assembly Binding redirect: How and Why?

There you can read what user @Evk wrote:

Why are binding redirects needed at all? Suppose you have application A that references library B, and also library C of version 1.1.2.5. Library B in turn also references library C, but of version 1.1.1.0. Now we have a conflict, because you cannot load different versions of the same assembly at runtime. To resolve this conflict you might use binding redirect, usually to the new version

So, QUICK FIX: Remove all entries in app.config.

In my case just by doing that program started working, but it will probably work only if you don't have any version conflicts of the same assembly at runtime.

If you do have such conflict you should fix these version numbers in app.config to match actually used versions of assemblies, but manual process is painful, so I suggest to auto-generate them again by opening Package Manager Console and perform packages reinstallation by typing Update-Package -reinstall

Hectocotylus answered 17/3, 2020 at 22:21 Comment(0)
W
3

This issue happens when you reference a .NET Standard project from a .NET 4.x project: none of the .NET Standard project's nuget package references are brought in as dependencies.

I resolved by add System.Runtime 4.3 and NETStandard.Library package and !!important!! I use refactor tool to look up the System.Runtime.dll version, It is 4.1.1.1 not 4.3 and then add an bindingRedirect in .config

<dependentAssembly>
    <assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="4.1.1.1" />
</dependentAssembly>
Wenoa answered 18/4, 2019 at 7:42 Comment(1)
Problem occurred for me after adding a projects as .NET standard to. Weirdly, despite removing the project, issues continues to exist.Cabalistic
C
3

it is too late I know, howewer there is no succesfully answer. I found the answer from another website. I fixed the issue when I delete the System.Runtime assemblydependency. I deleted this.

<dependentAssembly> <assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/> <bindingRedirect oldVersion="0.0.0.0-4.1.2.0" newVersion="4.1.2.0"/> </dependentAssembly>

Best Regards

Crossfade answered 23/9, 2019 at 18:10 Comment(0)
H
2

I had an issue with this in an NUnit 2.6.4 project targeting dotnet framework 4.6.2. I ran into that System.Runtime FileNotFound error trying to use Humanizer.

I fixed my error by installing NetStandard.Library into my unit test project.

Harvard answered 28/4, 2017 at 23:41 Comment(0)
G
2

We have found that AutoGenerateBindingRedirects might be causing this issue.

Observed: the same project targeting net45 and netstandard1.5 was successfully built on one machine and failed to build on the other. Machines had different versions of the framework installed (4.6.1 - success and 4.7.1 - failure). After upgrading framework on the first machine to 4.7.1 the build also failed.

Error Message:
 System.IO.FileNotFoundException : Could not load file or assembly 'System.Runtime, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
  ----> System.IO.FileNotFoundException : Could not load file or assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.

Auto binding redirects is a feature of .net 4.5.1. Whenever nuget detects that the project is transitively referencing different versions of the same assembly it will automatically generate the config file in the output directory redirecting all the versions to the highest required version.

In our case it was rebinding all versions of System.Runtime to Version=4.1.0.0. .net 4.7.1 ships with a 4.3.0.0 version of runtime. So redirect binding was mapping to a version that was not available in a contemporary version of framework.

The problem was fixed with disabling auto binding redirects for 4.5 target and leaving it for .net core only.

<PropertyGroup Condition="'$(TargetFramework)' == 'net45'">
  <AutoGenerateBindingRedirects>false</AutoGenerateBindingRedirects>
</PropertyGroup>
Galagalactagogue answered 16/3, 2018 at 13:9 Comment(0)
K
2

Ran into this just now in a Unit Test project after adding MsTest V2 through Nuget. Renaming app.config (so effectively removing it) did the trick for me.

Having read through all the above posts, I'm still not sure why, sorry!

Karankaras answered 1/9, 2018 at 8:19 Comment(0)
B
2

Before running the unit tests, just remove the runtime tags from app.config file. Problem will be solved.

Beaston answered 6/7, 2020 at 16:45 Comment(0)
T
2

In my case, I just deleted all the content in the packages folder in the solution's root directory.

I had tried to add a reference to a .NET Core 3.1 Class Library project to the solution having an ASP.NET 4.6.1 project. Then I started to get the same error except for the version: Could not load file or assembly 'System.Runtime, Version=4.2.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

Tirewoman answered 14/8, 2021 at 14:51 Comment(0)
P
1

I ended up in this situation several times with my .NET 4.6.1 web site. I created the problem each time when I added a reference to a separate .NET Core project. Upon building, Visual Studio correctly alerted me that such cross-framework references are invalid, and I quickly deleted the project reference. The project built fine after that, but the System.Runtime error appeared when accessing the web site and refused to go away.

The fix each time was lame but effective: I deleted the project directory and redownloaded it from source control. Even though there was no difference between before and after, I was able to build the project and access the page with no complaints.

Politicize answered 27/11, 2017 at 16:27 Comment(0)
P
1

I tried all the solutions here, but to no avail. Eventually, I solved it by opening the new csproj file and manually added the following section:

<Reference Include="System.Runtime, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<HintPath>..\packages\System.Runtime.4.3.0\lib\net462\System.Runtime.dll</HintPath>
</Reference>
Peevish answered 30/3, 2018 at 4:27 Comment(0)
V
1

I fixed the problem by removing the Nuget Package System.Runtime and then reinstalling it

Valeryvalerye answered 1/4, 2019 at 2:39 Comment(0)
L
0

I had a similar problem in VS 2017 15.45 - I found when I checked that even though the project compiled and ran it came up with a system.IO.FileNotFoundException with regard to System.Runtime when I tried to access TPL Dataflow objects.

When I checked the projects in the solution, one of them (the top one) was missing the System.Runtime package used by the underlying projects. Once I installed it from Nuget it all worked correctly.

Legalism answered 12/1, 2018 at 10:32 Comment(0)
P
0

I'm using ASP.Net CORE 2.1 and I got this error when I ran by selecting a .csproj from a list of about 40 in a big repo. When I opened the csproj file individually, the error was resolved. Something with how the program was launched was different when the csproj was opened.

Petrarch answered 24/5, 2018 at 13:53 Comment(0)
C
0

I solve this issue by switching from .NET 4.7.2 => .NET 4.5.2 and then switch back to 472. So in some cases this error occurs because package manager unable resolve dependences

Campaign answered 23/6, 2019 at 23:30 Comment(0)
M
0

If it's working previously, then there should be an App.config change. Undo App.config worked for me.

Membrane answered 24/6, 2019 at 15:5 Comment(0)
H
0

I had a project with the same problem , I solved it with change dotnet core version from 2.2 to 2.0, If your problem has remained , Try this solution

Hap answered 4/8, 2019 at 10:17 Comment(0)
T
0

I have also gone through this error and sharing how i got rid off to it.

In my case below line existed in web.config of webapi project but there was not package reference in package.config file.

Code in Web.config in Webapi Project

<dependentAssembly>
    <assemblyIdentity name="System.Runtime" publicKeyToken="B03F5F7F11D50A3A" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.1.2.0" newVersion="4.3.0" />
</dependentAssembly>

Code I Added in packages.config file in web api project Before closing of element.

<package id="System.Runtime" version="4.3.0" targetFramework="net461" />

Another Solution Worked in My Case:

Another Sure short that may work in case you copied project to another Computer system that may have little different package versions that you can try changing assembly version to version given in error on website / webapi when you run it. Like in this case as given in question Version needed is '4.1.0.0' so simply try changing current version in web.config to version shown in error as below

Error:

Could not load file or assembly 'System.Runtime, Version=4.1.0.0' or one of its dependencies

Version CHange

Tasha answered 29/12, 2019 at 12:18 Comment(0)
M
0

I had this error occur when building an Azure Function (with a queue trigger, should it make a difference)

The issue in this case was because the AzureFunctionsVersion was set to v2 instead of v3. To update it via VS2019, unload the project then edit the csproj file. Within the PropertyGroup node, add/edit the following:

<PropertyGroup>
  <AzureFunctionsVersion>v3</AzureFunctionsVersion>
</PropertyGroup>
Malayan answered 25/4, 2020 at 21:21 Comment(0)
B
0

For me, it was the missing 'web.config' file. After adding it to the deployed project directory in asp net core 3.1 app, the problem was solved.

Banderole answered 20/12, 2020 at 11:32 Comment(0)
A
0

I had the same issue closing and reopening the project fixed the issue for me.

Annunciate answered 5/1, 2022 at 20:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.