Yves Schelpe
Yves Schelpe

Reputation: 3473

netstandard1.x and winforms / wpf / windows - Could not load file or assembly System.Runtime, Version=4.1.0.0

I have setup a reusable base class library I wanted to use for both web, desktop and mobile. It holds basic functionaly, e.g. string, datetime manipulations etc.. Because I hate writing code twice.

VS2015 told me it was best to use the new ".NET Standard" platform, so I did that and it gave me this project.json

{
  "supports": {},
  "dependencies": {
    "Microsoft.NETCore.Portable.Compatibility": "1.0.1",
    "NETStandard.Library": "1.6.0"
  },
  "frameworks": {
    "netstandard1.5": {}
  }
}

Running unit tests with xunit went fine. Until I decided to use it (make a reference) in a WinForms app that targets net462 (yes some people still have clients that want WinForms). The application compiled a-ok, without any warnings or errors. But when I ran the application I got this error:

An exception of type 'System.IO.FileNotFoundException' occurred in DelegateIT.Core.Examples.Wizard.WinFormsClient.exe but was not handled in user code

Additional information: 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 different options, without success:

Should I just make a new project, a ".Net 4.6.2" class library and import the files as a link? Although I thought this whole new system would take this waste of time of project files per target framework away :(.


EDIT/ADDENDUM I had this code in the Wpf & WinForms client:

foreach (var line in csvLines) {
  var splittedLine = line.Split(';');
  splittedLines.Add(splittedLine.Select(s => CleanString(s)).ToList());
}

when I left out the CleanString method like this

foreach (var line in csvLines) {
  var splittedLine = line.Split(';');
  splittedLines.Add(splittedLine.Select(s => /*CleanString(*/s/*)*/).ToList());
}

then the error went away and the Wpf client and the WinForms client worked ok. It seems like System.Linq or at least System.Linq.Expressions is causing this error. But digging deeper I made sure to edit the CleanString method as well, and removed my extension method "RemoveMultipleWhiteSpaces".

As reference, this is the CleanString method and the reference extension method

    private string CleanString(string @string) {
      return @string
        .Trim()
        .RemoveMultipleWhitespaces();
    }

    public static string RemoveMultipleWhitespaces(this string @string) {
      if (@string == null) {
        return @string;
      }

      var cleanedString = Regex.Replace(@string, @"(\s)\s+", "$1");

     return cleanedString.Trim();
  }

Thus, to conclude, when I used the following code in CleanString, then it compiled and worked. So I'm not sure, is this System.Linq or sth else not being supported..?

private string CleanString(string @string) {
            return @string
                .Trim()
                /*.RemoveMultipleWhitespaces()*/;
        }

EDIT/ADDENDEUM2:

Tried this in project.json, but didn't work. Also tried adding "System.Runtime": "4.1.0" as dependency, not luck.

{
  "supports": {},
  "dependencies": {
    "Microsoft.NETCore.Portable.Compatibility": "1.0.1",
    "NETStandard.Library": "1.6.0",
    "Newtonsoft.Json": "9.0.1",
    "System.Linq": "4.1.0",
    "System.Linq.Expressions": "4.1.0",
    "System.Text.RegularExpressions": "4.1.0"
  },
  "frameworks": {
    "net462": {},
    "netstandard1.5": {}
  }
}

Upvotes: 2

Views: 436

Answers (2)

Sourcerer
Sourcerer

Reputation: 1906

You do not have to copy dependencies of .NET Standard libraries manually.

Microsoft finally admitted this is a problem and will fix it, expectantly, in NuGet version 4.0.1, the first update to NuGet 4 after VS 2017 ships.

The cleanest workaround now is to add <RestoreProjectStyle>PackageReference</RestoreProjectStyle> to a legacy project. However according to Rob Relyea MS will ignore this property after RTM so another workaround is <PackageReference Update="PlaceholderToConvinceThisProjectToGetTransitivePackageReferenceFromProjectReferences"/>.

Another workaround is to pack your .NET Standard library into NuGet package like in that answer, but it is not the easiest way.

Upvotes: 1

S&#248;ren
S&#248;ren

Reputation: 6787

I have the same problem (exactly same).
I just found the needed dlls in local nuget packages (in this case C:\Users\Soren\.nuget\packages\System.Reflection\4.1.0\lib\net462\System.Reflection.dll) and copied into the output of my project and the exception goes away.
but this is not the solution and solved the problem for a while.

Upvotes: 1

Related Questions