“Could not load file or assembly” error when net462 app references a netstandard1.5 library. But why?

僤鯓⒐⒋嵵緔 提交于 2019-12-10 11:03:23

问题


I am trying to figure out what I could be doing wrong in this sample project. I am getting an error when my net462 application references a netstandard1.5 library. The application has a dependency on "System.Collections.Immutable": "1.3.0", which targets NetStandard 1.0 according to Nuget. The library depends on "NETStandard.Library": "1.6.0".

Am I setting up either of these projects wrong? I would greatly appreciate any insight on this...

Here are their project.json :

app:

{
  "buildOptions": {
    "emitEntryPoint": true
  },
  "dependencies": {
    "SomeLibrary": "1.0.0-*"
  },
  "frameworks": {
    "net462": {
      "dependencies": {
        "System.Collections.Immutable": "1.3.0" 
      }
    }
  },
  "version": "1.0.0-*"
}

Library

{
  "buildOptions": {
    "allowUnsafe": true
  },
  "dependencies": {
  },
  "frameworks": {
    "netstandard1.5": {
      "dependencies": {
        "NETStandard.Library": "1.6.0"
      }
    }
  },
  "version": "1.0.0-*"
}

All the library has is this interface:

using System.Collections.Generic;

namespace SomeLibrary
{
    public interface SomeInterface
    {
        int GetValue(KeyValuePair<string, int> somePair);
    }
}

The app implements this interface and makes a call the concrete class:

public class Program
{
    public static void Main(string[] args)
    {
        var concreteObject = new ConcreteImplementation();
        var answer = concreteObject.GetValue(new KeyValuePair<string, int>("key", 33));
        Console.WriteLine(answer);
    }
}


class ConcreteImplementation : SomeInterface
{
    public int GetValue(KeyValuePair<string, int> somePair)
    {
        return somePair.Value;
    }
}

If I try to run the app, here is the error that I get:

{"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, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"}

Stack: at ErrorExample.Consumer..ctor() at ErrorExample.Program.Main(String[] args) in ..\ErrorExample\src\ErrorExample\Program.cs:line 11

What am I missing here? Thanks!


回答1:


I'm not quite sure about why this is happening, but using netstandard1.4 as a TFM for your library project would resolve your issue. In other words, project.json of your library should look like that:

{
  "buildOptions": {
    "allowUnsafe": true
  },
  "dependencies": {
  },
  "frameworks": {
    "netstandard1.4": { // <-- replace "netstandard1.5" with "netstandard1.4" or lower
      "dependencies": {
        "NETStandard.Library": "1.6.0"
      }
    }
  },
  "version": "1.0.0-*"
}

And as a current general rule of thumb: avoid using netstandard1.5 and netstandard1.6: use netstandard1.4 and lower according to your requirements until you are explicitly forced to. Wait for release of netstandard2.0. You may read the details about it in the MSDN blog artible about .NET Standard. And here's a FAQ.



来源:https://stackoverflow.com/questions/40856177/could-not-load-file-or-assembly-error-when-net462-app-references-a-netstandard

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