问题
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 ?
回答1:
.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() { ... }
}
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.
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 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.
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.
来源:https://stackoverflow.com/questions/52825255/referencing-aspnetcore-library-in-net-standard-target