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

落花浮王杯 提交于 2019-11-27 06:35:18
Ty Petrice

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.

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.

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.

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

Marco

I fixed it by deleting my app.config with

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

entries.

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

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.

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>

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>

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

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.

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.

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!

This issue has many causes... in my case the problem was that was 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.

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

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

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.

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>

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

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>

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!