ChatGPT解决这个技术问题 Extra ChatGPT

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

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:

Reference Std library as project reference. Error: gives me the previous error. 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. 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.


A
Alpha

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.


Or just add the reference to your web.config: stackoverflow.com/a/38603514/1145177
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...
I had the same problem like @DavidRogers in a MSTest project. Consolidating the differences between app.config and packages.config resolved the problem.
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 etc
Solution worked for me. Problem started after installing HtmlAgilityPack NUGET. And would not run because of incorrect version info in packages. +1
T
Tushar

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.


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.
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.2
Based on your answer I checked my nuget packages and found 'Google.protobuf' needs (Consolidating) between my projects, thnx
C
Cory Nelson

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.


Which update of Visual Studio 17? Can you specify the version?
I still have the problem in 15.5.5 VS2017. It looks like there are other causes.
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.
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. docs.microsoft.com/en-us/nuget/reference/…
Crucial: after updating the toolchain, you need to regenerate the assembly bindings. This will remove the System.Runtime reference from them.
S
Sheena Agrawal

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


A better explanation of why this would work would be helpful.
The problem with this method is, whenever you update any nuget package or add new nuget package, it'll get added again.
N
Noffls

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

https://i.stack.imgur.com/YfuGh.png

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>

Weirdly, I fixed the problem for me by removing all <dependentAssembly> nodes for System.Runtime..
@MattBrewerton confirmed!
P
Paul Roub

I fixed it by deleting my app.config with

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

entries.

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


This worked for me! Definitely try this if all other items are not working out for you
M
Michele Bortot

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>

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).
p
psychoboi111

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


s
shine

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>

Problem occurred for me after adding a projects as .NET standard to. Weirdly, despite removing the project, issues continues to exist.
M
Mesut erdoğan

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


y
yu yang Jian

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.


U
Unheilig

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.


u
user1921819

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>

j
jgmdavies

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!


s
spamguy

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.


M
Mike Chamberlain

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>

D
Darren

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


S
Sameer Sayani

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


k
karr

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


L
Liam

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.


u
user5389726598465

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.


О
Олег Железцов

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


D
Damitha

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


k
keivan kashani

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


H
Heemanshu Bhalla

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

https://i.stack.imgur.com/Jsenu.png


R
Rory McCrossan

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>

A
Abhinandan

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.


b
bakalolo

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