Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not load file or assembly System.Runtime, Version=4.1.2.0

Suddenly, after adding some NuGet packages (mostly, related to ASPNET Identity), it started showing this error:

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

I have .Net Framework 4.7.1 targeted. I tried installing NuGet package System.Runtime 4.3.0, it didn't help. The web.config file has a reference:

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

There's no System.Runtime.dll in the bin folder.

Any ideas?

I use Visual Studio 2017 15.5.5.

UPDATE:

I use PackageReference entries in the .csproj file, so it's not the issue with the packages.config.

It seems like some dependencies are not loaded.

like image 387
Andrey Korneyev Avatar asked Jan 29 '18 14:01

Andrey Korneyev


People also ask

Can not load file or assembly?

In summary if you get the "Could not load file or assembly error", this means that either your projects or their references were built with a reference to a specific version of an assembly which is missing from your bin directory or GAC.

What is System Runtime CompilerServices unsafe?

Runtime. CompilerServices. Unsafe state that it: Contains generic, low-level functionality for manipulating pointers.


3 Answers

I had this recently upgrading a project from net462 to net471, the issue in my case was some assembly redirects which were required by the net462 version but badly confused the net471.

The solution was to remove all the assembly redirect entries in web.config and let Visual Studio recompute them - they will appear as a warning which may be clicked on to re-add them to the web.config

like image 77
Paul Hatcher Avatar answered Sep 21 '22 07:09

Paul Hatcher


My ASP.NET project was already on net471 in VS 15.8.4 when this started happening. When I attempted to update my existing NuGet packages to the latest versions I would receive this error upon launching the project in IIS Express.

BadImageFormatException: Could not load file or assembly 'System.Runtime' or one of its dependencies.

I was able to resolve this issue by modifying my project's web.config file.

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

Deleting the bindingRedirect lines for both of these System.Runtime dependencies resolved this issue in my project leaving me with this in my web.config.

  <dependentAssembly>     <assemblyIdentity name="System.Runtime.Serialization.Primitives" publicKeyToken="B03F5F7F11D50A3A" culture="neutral" />   </dependentAssembly>   <dependentAssembly>     <assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />   </dependentAssembly> 
like image 23
UncheckedError Avatar answered Sep 22 '22 07:09

UncheckedError


The Assembly Binding's might be wrong.

DELETE the app.config or web.config AssemblyBinding sections. Then run this BindingRedirect in the Package Manager window:

Get-Project –All | Add-BindingRedirect

Optional: If you upgrade the framework version of all projects, you will need to reinstall all Nuget packages for all projects. To do that easily use this package managers script:

Get-Project –All | % { Get-Package -ProjectName $_.ProjectName | % { update-package $_.Id -reinstall -ProjectName $_.ProjectName -ignoreDependencies } }

Then run the Binding Redirect code above.

like image 29
OzBob Avatar answered Sep 21 '22 07:09

OzBob