net462应用程序引用netstandard1.5库时出现“无法加载文件或程序集”错误。 但为什么?

我试图弄清楚在这个示例项目中我可能做错了什么。 当我的net462应用程序引用netstandard1.5库时,我收到错误。 该应用程序依赖于"System.Collections.Immutable": "1.3.0" ,它根据Nuget定位NetStandard 1.0。 该库依赖于"NETStandard.Library": "1.6.0"

我设置这些项目中的任何一个都错了吗? 我非常感谢对此有任何见解……

这是他们的project.json:

应用:

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

图书馆

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

所有库都有这个界面:

 using System.Collections.Generic; namespace SomeLibrary { public interface SomeInterface { int GetValue(KeyValuePair somePair); } } 

该应用程序实现此接口并调用具体类:

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

如果我尝试运行该应用程序,这是我得到的错误:

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

我在这里想念的是什么? 谢谢!

我不太清楚为什么会这样,但使用netstandard1.4作为您的图书馆项目的TFM将解决您的问题。 换句话说,您的库的project.json应该如下所示:

 { "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-*" } 

并且作为当前的一般经验法则:避免使用netstandard1.5netstandard1.6 :根据您的要求使用netstandard1.4和更低版本,直到您被明确强制为止。 等待netstandard2.0发布。 您可以在关于.NET Standard的MSDN博客中阅读有关它的详细信息。 这是一个FAQ 。