本文介绍了“无法加载文件或程序集"net462 应用程序引用 netstandard1.5 库时出错.但为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄清楚在这个示例项目中我可能做错了什么.当我的 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 :

应用:

{
  "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<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, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' 或其依赖项之一.系统找不到指定的文件.":"System.Runtime, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"}

堆栈:在 ErrorExample.Consumer..ctor()在 ErrorExample.Program.Main(String[] args) in ..ErrorExamplesrcErrorExampleProgram.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.5netstandard1.6:使用 netstandard1.4 并根据您的要求降低,直到您明确被迫这样做.等待netstandard2.0的发布.您可以在 MSDN 博客中阅读有关它的详细信息关于 .NET Standard 的文章.这是常见问题解答.

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 库时出错.但为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-02 15:39