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

查看:115
本文介绍了在.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 / etc中的代码接口。是该接口的实现。就像在代码中使用接口一样,您可以将接口的任何实现强制转换为接口,但是,您只能使用该接口提供的功能的最小公分母。代码中的基本思想:

.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目标(通过使用指令等)。无论哪种情况,都可以安全地包含在.NET Standard库中,因为它仅使用.NET Standard支持的功能。尽管我不知道具体情况如何,但是完全有可能像ASP.NET Core NuGet包那样,而您实际上不能包含在.NET Standard库中。如果它不是专门针对.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标准库只能真正地依赖于其他.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天全站免登陆