问题描述
我试图弄清楚在这个示例项目中我可能做错了什么。当我的 net462
应用程序引用 netstandard1.5
库时,出现错误。该应用程序依赖于 System.Collections.Immutable: 1.3.0
,根据Nuget的目标是NetStandard 1.0。该库取决于 NETStandard.Library: 1.6.0
。
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...
这是他们的project.json:
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:
{无法加载文件或程序集'System.Runtime,版本= 4.1.0.0,区域性=中性,PublicKeyToken = b03f5f7f11d50a3a'或其依赖项之一。系统找不到指定的文件。: System.Runtime,版本= 4.1.0.0,Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a}
堆栈:在ErrorExample.Consumer..ctor()
在ErrorExample.Program.Main(String [] args)在..\ErrorExample中\src\ErrorExample\Program.cs:line 11
我在这里缺少什么?
谢谢!
What am I missing here?Thanks!
推荐答案
我不太确定为什么会发生这种情况,但是使用 netstandard1.4
作为库项目的TFM将解决您的问题。换句话说,库的 project.json
应该看起来像这样:
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-*"
}
根据目前的一般经验,避免使用 netstandard1.5
和 netstandard1.6
:使用 netstandard1.4
并根据需要降低,直到您明确被迫。等待发布 netstandard2.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.
这篇关于“无法加载文件或程序集” net462应用程序引用netstandard1.5库时出错。但为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!