如何正确引用 NETStandard 2.1?

huangapple go评论65阅读模式
英文:

How do I properly reference NETStandard 2.1?

问题

我看到有评论说我需要'需要 Core 3.0',但我不确定确切的含义是什么。Core 3.0已安装在我的系统上,如果我想要的话,我可以创建一个Core 3.0项目,但据我所知,我不能创建一个同时针对Core 3.0和.NET Standard 2.1的项目。

目前我的项目(类库)的目标框架是.NET Standard 2.1。编译没有问题,但当我运行引用它的项目(可执行文件)时,出现错误:

无法加载文件或程序集'netstandard,版本=2.1.0.0,文化=中性,公钥令牌=cc7b13ffcd2ddd51'或其依赖项之一。系统找不到指定的文件。

这个可执行文件是一个.NET Framework 4.7项目。我需要我的dll在.NET Framework和Core项目中都能运行的原因是什么。

有没有什么诀窍可以使这正常工作?是否有一种不同的方法来创建.NET Standard 2.1项目?或者是否有依赖文件需要我本地复制?

英文:

I've seen comments stating that I will 'need Core 3.0' but I'm not sure what that means exactly. Core 3.0 is installed on my system and I can create a Core 3.0 project if I want to but as far as I know I can't create a project that targets both Core 3.0 and .NET Standard 2.1.

Currently my project (class lib) target framework is .NET Standard 2.1. It compiles fine but when I run the project (exe) that references it I get an error:

Could not load file or assembly 'netstandard, Version=2.1.0.0, Culture=neutral, 
PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the file 
specified.

The exe is a NET Framework 4.7 project. The reason that I need my dll to run under NET Standard is that it needs to be referenced in both NET Framework and Core projects.

Is there a trick to getting this to work properly? Is there a different way to create a NET Standard 2.1 project? Or are there dependency files that I need to copy locally?

答案1

得分: 1

在从.NET Framework项目导入.NET Standard项目时遇到了此问题。要解决此问题,请在您的 Web.config 文件中添加以下内容,并将我的版本4.7.1和2.0更改为您所需的版本:

<system.web>
    <compilation debug="true" targetFramework="4.7.1">
        <assemblies>
            <add assembly="netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51" />
        </assemblies>
    </compilation>
    <httpRuntime targetFramework="4.7.1"/>
    ...
</system.web>
英文:

Had that issue when importing a net standart project from a net framework project. To fix put inside your Web.config the following, change my versions 4.7.1 and 2.0 to your needed ones:

  &lt;system.web&gt;
    &lt;compilation debug=&quot;true&quot; targetFramework=&quot;4.7.1&quot;&gt;
      &lt;assemblies&gt;
        &lt;add assembly=&quot;netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51&quot; /&gt;
      &lt;/assemblies&gt;
    &lt;/compilation&gt;
    &lt;httpRuntime targetFramework=&quot;4.7.1&quot;/&gt;
...
  &lt;/system.web&gt;

huangapple
  • 本文由 发表于 2020年1月4日 01:37:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/59582938.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定