在 .Net 标准目标中引用 AspNetCore 库 [英] Referencing AspNetCore Library In .Net Standard Target

查看:18
本文介绍了在 .Net 标准目标中引用 AspNetCore 库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我的前辈编写的 N 层应用程序中学习.数据访问和业务层的目标框架是 .NET Standard 2.0 但在这些层的依赖项中有来自 Microsoft.AspNetCore 的库!为什么 .Net Standart 目标可以引用 .Net Core 库?

I am trying to learn from an N-tier application written by my senior. Target Framework of Data Access and Business layers are .NET Standard 2.0 but inside dependencies of that layers there are libraries from Microsoft.AspNetCore ! How come a .Net Standart target can reference .Net Core libraries ?

推荐答案

.NET Standard 只是对支持的 API 的编码,有点像代码中的接口,带有 .NET Framework/.NET Core/等.是该接口的实现.就像在代码中使用接口一样,您可以将接口的任何实现转换为接口,但是,您只能使用该接口提供的功能的最小公分母.代码中的基本思想:

.NET Standard is just a codification of supported APIs, kind of like an interface in code, with .NET Framework/.NET Core/etc. being the implementations of that interface. Just as with using an interface in code, you can cast any implementation of the interface to the interface, but then, you may only use the least common denominator of functionality that that interface provides. The basic idea in code:

public interface INetStandard
{
    public void StandardMethod();
}

public class NetFramework : INetStandard
{
    public void StandardMethod() { ... }
    public void FrameworkMethod() { ... }
}

public class NetCore : INetStandard
{
    public void StandardMethod() { ... }
    public void CoreMethod() { ... }
}

定位 .NET Standard 类似于向上转换到接口.在上面的代码中,这意味着您可以访问StandardMethod,但您将不能使用FrameworkMethod/CoreMethod>,不管实际类型是什么.

Targeting .NET Standard is similar to upcasting to the interface. In the code above, that means you could access StandardMethod, but you would not be able to use FrameworkMethod/CoreMethod, regardless of what the actual type was.

就在 .NET Standard 中使用 .NET Core 库之类的东西而言,这实际上并不是你在做什么.像您提到的 NuGet 包这样的库通常是多目标的,例如同时使用 .NET Standard 和 .NET Core 作为目标.这实际上是一个承诺,即该库虽然适用于 .NET Core,但不使用任何特定于 .NET Core 的 API.或者,如果是这样,它会以不会破坏 .NET Standard 目标的方式(通过 using 指令等)这样做.无论哪种情况,都可以安全地包含在 .NET Standard 库中,因为它只使用 .NET Standard 支持的内容.尽管我不知道具体情况,但完全有可能拥有您实际上不能包含在 .NET Standard 库中的 ASP.NET Core NuGet 包之类的东西.如果它没有专门针对 .NET Standard,那么它将无法工作.

As far as using something like a .NET Core library in .NET Standard goes, that's not actually what you're doing. Libraries like the NuGet packages you mention are generally multi-targeted, such as using both .NET Standard and .NET Core as targets. This is effectively a promise that the library, although intended for .NET Core, does not use any .NET Core-specific APIs. Or, if it does, it does so in a way that would not break the .NET Standard target (via using directives and such). In either case, it's safe to include in a .NET Standard library because it only uses things that .NET Standard supports. Although I don't know of specific cases off the top of my head, it's entirely possible to have something like an ASP.NET Core NuGet package that you actually cannot include in a .NET Standard library. If it doesn't specifically target .NET Standard, then it won't work.

.NET Framework 的工作方式类似,但也是一种特殊情况.由于有许多针对 .NET Framework 的旧库实际上与 .NET Standard 完全兼容,因此 Visual Studio 特意允许您将它们放入其中,即使它们没有专门针对 .NET Standard.然而,当你这样做时,你会收到一个警告,这是一个温和的提醒,仅仅因为你被允许这样做,并不意味着它实际上会起作用.然后,您必须进行自己的测试,以确保库正常运行并且一切都正确编译.这样,旧的库就不会被强制全部升级,特别是因为许多库现在实际上可能不受支持或被放弃.

.NET Framework works similarly, but is also a special case. Because there are so many older libraries targeting .NET Framework that are in fact perfectly compatible with .NET Standard, Visual Studio makes a special point of allowing you to drop them in, even though they do not specifically target .NET Standard. However, when you do this, you get a warning, which serves as a gentle reminder that just because you're being allowed to do this, it doesn't mean it's actually going to work. You have to then do your own testing to ensure that the library functions as it should and everything compiles correctly. This way, older libraries are not force to all upgrade, especially since many may actually be unsupported or abandoned now.

总而言之,一个 .NET Standard 库实际上只能依赖于其他 .NET Standard 库.您可以使用的 Core 包是这样的,因为除了 .NET Core 之外,它们还针对 .NET Standard.由于特殊例外,允许仅针对 .NET Framework 的库并且不能保证正常工作.

Long and short, a .NET Standard library can truly only depend on other .NET Standard libraries. The Core packages that you can utilize are such because they target .NET Standard, in addition to .NET Core. Libraries that only target .NET Framework are allowed because of a special exception and not guaranteed to work.

这篇关于在 .Net 标准目标中引用 AspNetCore 库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆