44

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.

Andrey Korneyev
  • 475
  • 1
  • 4
  • 12
  • 13
    Any news on this one - it is killing me!! – MoonKnight Mar 11 '18 at 18:54
  • I'm super confused about this one. I installed Application Insights and it gave me exactly this. So why did it not install what it needed. The kicker is if I just comment this out it tells me that 4.1.2 is installed anyway - even after restarting Visual Studio. – Simon_Weaver Aug 24 '18 at 21:55

6 Answers6

38

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.

OzBob
  • 4,227
  • 1
  • 39
  • 48
  • 2
    Wish I could upvote this to the top answer. Wish I had found that "Optional" bit before I did the conversion of all projects from .NET 4.6 to target .NET 4.8... it would have saved me tons of time wrestling with the infuriating NuGet Package Manager. The Package Console is the better approach, but is so poorly documented, it is hard to figure out what you need to do. – Brian Kennedy Mar 04 '22 at 02:47
  • 1
    You're my hero OzBob! Microsoft should pay you for this! If I could have seen this post 3 days ago, the world would be a better place today. – Yster May 10 '22 at 07:12
31

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

Niels R.
  • 7,260
  • 5
  • 32
  • 44
Paul Hatcher
  • 7,342
  • 1
  • 54
  • 51
25

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>
UncheckedError
  • 351
  • 5
  • 6
  • 2
    I'd recommend re-assembling the project from scratch, doing everything the proper way, little by little, and observing if anything wrong happens at each step. – Andrey Korneyev Sep 14 '18 at 22:48
7

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

The package System.Runtime is referenced but it needs to be installed.

  • In Visual Studio's menu, go to Tool > NuGet Package Manager > Manage NuGet Packages for Solution...

  • In the active NuGet - Solution window, go to the tab Installed, navigate to System.Runtime by Microsoft. Select with a single-click and look on the side window if a version exists for the project. If not, select the project and click Install.

  • Build solution.

Alfred Wallace
  • 1,741
  • 1
  • 14
  • 32
  • 1
    Thx, this might work in some cases. Helped in mine. I wanted to mock User.Identity in unit tests ( var context = new Mock(); var mockIdentity = new Mock(); ) with Moq nuget package. After the package intallation System.Runtime library was referenced but dll itself was missing in the project. – fxdx Jul 19 '18 at 10:47
3

I had this issue and it took me a while to get to the bottom of it. Someone else may be having a similar setup to mine and hence having this issue.

I was running a .NET Framework v4.8 app in IIS (WCF Service). It was working fine until I accidently copied in a .NET Core 3.1 DLL into the bin directory. IIS was obviously trying to host the .NET Core DLL and the runtimes did not exist. Once I Deleted the DLL, it with all hunky dory. Hope this helps someone else :)

Srini
  • 446
  • 5
  • 11
0

I have resolved this by adding the following to the web.config:

<compilation debug="true">
  <assemblies>
    <add assembly="System.Runtime, Version=4.0.20.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
  </assemblies>
</compilation>

Your specific assembly version may vary.

DxCK
  • 4,402
  • 7
  • 50
  • 89