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 ?
If you are not sure which version of . NET Standard you should target, go with 2.0 as it offers a balance of reach and APIs available for you to use. If your goal is to make your library usable for many frameworks as possible while gettings all the APIs you can from .
Add Class Library Reference To use a class library in your application, you must add a reference to the library to access its functionality. Right click on the project name of your console app in Solution Explorer and select Add ->Reference option.
once you add the requisite framework and build, you can reference the . NET CORE project from your . NET 4.6 application directly.
Open Visual Studio 2015 Go to menu click File> New> Project then choose . net Core from left menu by selecting ASP.NET Core Web Application like below image. I have chosen an ASP.NET Core sample Template, like the below image. Let's create another project as a simple class library.
To reference ASP.NET Core, add the following <FrameworkReference> element to your project file: Referencing ASP.NET Core in this manner is only supported for projects targeting .NET Core 3.x. For information on using ASP.NET Core 5.0 and later APIs in a class library, see this GitHub issue.
Supported target frameworks Target Framework TFM .NET 5+ (and .NET Core) netcoreapp1.0 netcoreapp1.1 netcoreapp2. ... .NET Standard netstandard1.0 netstandard1.1 netstandar ... .NET Framework net11 net20 net35 net40 net403 net45 net ... Windows Store netcore [netcore45] netcore45 [win] [win ... 4 more rows ...
With .NET Core, you can build cross-platform console apps and ASP.NET Core Web applications and cloud services. .NET Standard: This is the set of fundamental APIs (commonly referred to as base class library or BCL) that all .NET implementations must implement.
.NET Core and .NET Framework are different frameworks; I wouldn't expect this to work; if you want a library that is consumable from both, AFAIK it needs to either multi-target (i.e. have .NET Core and .NET Framework targets), or target .NET Standard.
.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With