Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error referencing Net Standard from Net 4.6.1 / 4.7

I have a web app initially targeting 4.6.1. This web app references a class library; in turn, this references a Net Standard 1.6 class library.

Adding the reference to the Net Standard library was the latest change, and appears to have caused it to break: the web app was working fine until the Net Standard library was introduced; at which point, I started getting the 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.

System.Runtime is included as a NuGet package in the web app.

It may not be relevant, but when I look in the packages directory; I see System.Runtime.4.3.0\lib has net45 and net462 directories, but not 4.6.1 or 4.7 (see below for why I would expect this).

Some things that I've tried that haven't worked:

  • Despite seeing here that 4.6.1 support Net Std 1.6, I did try upgrading to 4.7, but to no avail.
  • Tried setting up an assembly redirect in the web.config. This changes the error slightly to say that it's looking for 4.0.0.0 and then the re-direct kicks in, but it's essentially the same error.
  • Tried removing the packages directory, to see if I could force a refresh (also tried Update-Package on System.Runtime).
  • On searching the web, I've found a couple of similar issues; one on GitHub (that I can't seem to relocate) implied that an update to VS2017 would fix the problem.

There's enough recent stuff on the web that makes me think I'm not the only person to experience this; although I haven't found any explanation as to what might be the cause. Can anyone offer an explanation as to what might be causing this, or any diagnostic steps that I can use?

like image 489
Paul Michaels Avatar asked Oct 20 '17 15:10

Paul Michaels


2 Answers

After a long converting process, which I described here I have the experience: you have to install the NuGet package NETStandard.Library also in your main project (in your webapp .net 4.6.1). It will install a plenty of packages.

I use NETStandard.Library version 1.6.1.

If you use System.Runtime.Serialization in your netstandard code, you have to install System.Runtime.Serialization.Primitives and System.Runtime.Serialization.Xml or .Json in the main project, too.

And also all other references of the netstandard project should be referenced in the main project, too.

like image 132
lvoros Avatar answered Sep 23 '22 19:09

lvoros


I had a similar problem and I solved it by removing the System.Runtime reference from the project and install it via Nuget (Net Standard compatible). It wasn't the only package that had problems (other example, System.Net.Http). Also, if you have multiple projects in your solution, you should install Net Standard on every one of them.

like image 30
Lavinia N. Avatar answered Sep 23 '22 19:09

Lavinia N.