ChatGPT解决这个技术问题 Extra ChatGPT

What is the difference between .NET Core and .NET Standard Class Library project types?

In Visual Studio, there are at least three different types of class libraries you can create:

Class Library (.NET Framework)

Class Library (.NET Standard)

Class Library (.NET Core)

While the first is what we've been using for years, a major point of confusion I've been having is when to use the .NET Standard and .NET Core class library types. I've been bitten by this recently when attempting to multi-target different framework versions, and creating a unit test project.

So, what is the difference between Class Library (.NET Standard) and Class Library (.NET Core), why do both exist, and when should we use one over the other?

You missed one: Class Library (Portable). Core == framework, .NET Standard == portable.
There was one from Xamarin too, but these other ones don't add any value to the question :)
Well, they do. The core idea is the they gave up on the portable approach, it suffered too heavily from the n! problem with way too many profiles. So now you've got 7 standards to choose from. Most are not actually portable right now :) .NETCore is not done by a long shot, probably takes another two years at the clip they are going.
OP said "at least 3 different types". The post was accurate.
I was confused by the naming of Core which is not a core subset of neither the Standard nor the Framework plateform. Also we regularly see ASP associated with .Net Core. This is also very confusing...

S
Shaun Luttin

When should we use one over the other?

The decision is a trade-off between compatibility and API access.

Use a .NET Standard library when you want to increase the number of applications that will be compatible with your library, and you are okay with a decrease in the .NET API surface area your library can access.

Use a .NET Core library when you want to increase the .NET API surface area your library can access, and you are okay with allowing only .NET Core applications to be compatible with your library.

For example, a library that targets .NET Standard 1.3 will be compatible with applications that target .NET Framework 4.6, .NET Core 1.0, Universal Windows Platform 10.0, and any other platform that supports .NET Standard 1.3. The library will not have access to some parts of the .NET API, though. For instance, the Microsoft.NETCore.CoreCLR package is compatible with .NET Core, but not with .NET Standard.

What is the difference between Class Library (.NET Standard) and Class Library (.NET Core)?

Compatibility: Libraries that target .NET Standard will run on any .NET Standard compliant runtime, such as .NET Core, .NET Framework, Mono/Xamarin. On the other hand, libraries that target .NET Core can only run on the .NET Core runtime.

API Surface Area: .NET Standard libraries come with everything in NETStandard.Library, whereas .NET Core libraries come with everything in Microsoft.NETCore.App. The latter includes approximately 20 additional libraries, some of which we can add manually to our .NET Standard library (such as System.Threading.Thread) and some of which are not compatible with the .NET Standard (such as Microsoft.NETCore.CoreCLR).

Also, .NET Core libraries specify a runtime and come with an application model. That's important, for instance, to make unit test class libraries runnable.

Why do both exist?

Ignoring libraries for a moment, the reason that .NET Standard exists is for portability; it defines a set of APIs that .NET platforms agree to implement. Any platform that implements a .NET Standard is compatible with libraries that target that .NET Standard. One of those compatible platforms is .NET Core.

Coming back to libraries, the .NET Standard library templates exist to run on multiple runtimes (at the expense of API surface area). Conversely, the .NET Core library templates exist to access more API surface area (at the expense of compatibility) and to specify a platform against which to build an executable.

Here is an interactive matrix that shows which .NET Standard supports which .NET implementation(s) and how much API surface area is available.


Very good answer. An additional question though (relates to this question: why is an application model necessary to run unit tests? This was never the case in the past, when we used non-runnable class libraries to hold collections of unit tests.
I've updated my answer to the linked question. TL;DR; In the past, class libraries targeted the full framework, which includes an application model.
You forgot to cover Class Library (.NET Framework), is it compatible with .NET Standard and .NET Core?
This diagram really helped me get it.
@BerBar The original question was about the difference between .NET Standard and .NET Core. That's why I omitted cross-platform details, because cross-platform is not a difference between Core and Standard. I've intentionally kept my answer scoped to the original question.
P
Peter Mortensen

A .NET Core Class Library is built upon the .NET Standard. If you want to implement a library that is portable to the .NET Framework, .NET Core and Xamarin, choose a .NET Standard Library

.NET Core will ultimately implement .NET Standard 2 (as will Xamarin and .NET Framework)

.NET Core, Xamarin and .NET Framework can, therefore, be identified as flavours of .NET Standard

To future-proof your applications for code sharing and reuse, you would rather implement .NET Standard libraries.

Microsoft also recommends that you use .NET Standard instead of Portable Class Libraries.

To quote MSDN as an authoritative source, .NET Standard is intended to be One Library to Rule Them All. As pictures are worth a thousand words, the following will make things very clear:

1. Your current application scenario (fragmented)

Like most of us, you are probably in the situation below: (.NET Framework, Xamarin and now .NET Core flavoured applications)

https://i.stack.imgur.com/Dwrzt.png

2. What the .NET Standard Library will enable for you (cross-framework compatibility)

Implementing a .NET Standard Library allows code sharing across all these different flavours:

https://i.stack.imgur.com/tE1ny.png

For the impatient:

.NET Standard solves the code sharing problem for .NET developers across all platforms by bringing all the APIs that you expect and love across the environments that you need: desktop applications, mobile apps & games, and cloud services: .NET Standard is a set of APIs that all .NET platforms have to implement. This unifies the .NET platforms and prevents future fragmentation. .NET Standard 2.0 will be implemented by .NET Framework, .NET Core, and Xamarin. For .NET Core, this will add many of the existing APIs that have been requested. .NET Standard 2.0 includes a compatibility shim for .NET Framework binaries, significantly increasing the set of libraries that you can reference from your .NET Standard libraries. .NET Standard will replace Portable Class Libraries (PCLs) as the tooling story for building multi-platform .NET libraries.

For a table to help understand what the highest version of .NET Standard that you can target, based on which .NET platforms you intend to run on, head over here.

Sources: MSDN: Introducing .NET Standard


ASP.NET Core is a little misplaced in that graphic, as it can be used with the full .NET Framework, not only .NET Core, because it actually targets .NET Standard.
But you can create an ASP.NET Core app with the full .NET Framework - ASP.NET Core really belongs to the same layer as .NET Standard does. It's not constrained to just .NET Core.
@Neme Firstly, yes .Net Core can include .Net Framework libraries but losing cross-platform reuse(only for Windows - not *nix or OSX, or reuse in Xamarin). A situation which was catered for given that many have & want to reuse existing libraries written thein full.Net Framework with no interest for cross-platform benefits (OS-level and App-Model level)...If you still feel I'm wrong, perhaps you can argue with Microsoft, who authored those images... :-)
I'm not talking about combining .NET Core & .NET Framework. My point is that ASP.NET Core isn't dependent on .NET Core at all, despite the name. It is written as a library that targets .NET Standard, therefore you can use it everywhere you can use .NET Standard. Yes, they made a mistake in that image.
@OgrishMan You cannot create an executable in .Net Standard. It can only be a Class Library, that can be referenced by other executing code. It has no runtime.
P
Peter Mortensen

The short answer would be:

IAnimal == .NetStandard (General)
ICat == .NetCore (Less general)
IDog == .NetFramework (Specific / oldest and has the most features)

@Joe.wang as I see it is bad that it messes the relationship between .NET Core and .NET Framework. If .NET Core is the bird, then .NET Framework cannot be the eagle (maybe cat is more suitable).
@LexLi is right, this muddies the waters. .NET Framework is not a subtype of .NET Core.
this may look bit fancy but not acurate
The original comment by @Joe sounded more accurate. edited answer by Community made it confusing
Dogs have more features than cats? Nop :)
J
Joel Coehoorn

.NET and .NET Core are two different implementations of the .NET runtime. Both Core and Framework (but especially Framework) have different profiles that include larger or smaller (or just plain different) selections of the many APIs and assemblies Microsoft has created for .NET, depending on where they are installed and in what profile.

For example, there are some different APIs available in Universal Windows apps than in the "normal" Windows profile. Even on Windows, you might have the "Client" profile vs. the "Full" profile. Additionally, and there are other implementations (like Mono) that have their own sets of libraries.

.NET Standard is a specification for which sets of API libraries and assemblies must be available. An app written for .NET Standard 1.0 should be able to compile and run with any version of Framework, Core, Mono, etc., that advertises support for the .NET Standard 1.0 collection of libraries. Similar is true for .NET Standard 1.1, 1.5, 1.6, 2.0, etc. As long as the runtime provides support for the version of Standard targeted by your program, your program should run there.

A project targeted at a version of Standard will not be able to make use of features that are not included in that revision of the standard. This doesn't mean you can't take dependencies on other assemblies, or APIs published by other vendors (i.e.: items on NuGet). But it does mean that any dependencies you take must also include support for your version of .NET Standard. .NET Standard is evolving quickly, but it's still new enough, and cares enough about some of the smaller runtime profiles, that this limitation can feel stifling. (Note a year and a half later: this is starting to change, and recent .NET Standard versions are much nicer and more full-featured).

On the other hand, an app targeted at Standard should be able to be used in more deployment situations, since in theory it can run with Core, Framework, Mono, etc. For a class library project looking for wide distribution, that's an attractive promise. For an end-user-focused project intended mainly for an internal audience, it may not be as much of a concern.

.NET Standard can also be useful in situations where the system administrator team is wanting to move from ASP.NET on Windows to ASP.NET for .NET Core on Linux for philosophical or cost reasons, but the Development team wants to continue working against .NET Framework in Visual Studio on Windows.


While a good overview of what .NET Core and .NET Standard are, this answer fails to answer the question about class libraries targeting each of these.
If that's your goal, the question needs to be closed as either "unclear what you're asking", since there are always going to be too many situational specifics that play into a given person's environment for us to ever just tell you what to do, or as "Too Broad" if you're asking about the general case. All we can do here is give you info about the products, so you can be informed for making your own decision.
That is clearly not the case, as another has accurately answered the question. My question was about the class libraries. Your answer was about the frameworks.
P
Peter Mortensen

.NET Framework and .NET Core are both frameworks.

.NET Standard is a standard (in other words, a specification).

You can make an executable project (like a console application, or ASP.NET application) with .NET Framework and .NET Core, but not with .NET Standard.

With .NET Standard you can make only a class library project that cannot be executed standalone and should be referenced by another .NET Core or .NET Framework executable project.


P
Peter Mortensen

I hope this will help to understand the relationship between .NET Standard API surface and other .NET platforms. Each interface represents a target framework and methods represents groups of APIs available on that target framework.

namespace Analogy
{
    // .NET Standard

    interface INetStandard10
    {
        void Primitives();
        void Reflection();
        void Tasks();
        void Xml();
        void Collections();
        void Linq();
    }

    interface INetStandard11 : INetStandard10
    {
        void ConcurrentCollections();
        void LinqParallel();
        void Compression();
        void HttpClient();
    }

    interface INetStandard12 : INetStandard11
    {
        void ThreadingTimer();
    }

    interface INetStandard13 : INetStandard12
    {
        //.NET Standard 1.3 specific APIs
    }

    // And so on ...


    // .NET Framework

    interface INetFramework45 : INetStandard11
    {
        void FileSystem();
        void Console();
        void ThreadPool();
        void Crypto();
        void WebSockets();
        void Process();
        void Drawing();
        void SystemWeb();
        void WPF();
        void WindowsForms();
        void WCF();
    }

    interface INetFramework451 : INetFramework45, INetStandard12
    {
        // .NET Framework 4.5.1 specific APIs
    }

    interface INetFramework452 : INetFramework451, INetStandard12
    {
        // .NET Framework 4.5.2 specific APIs
    }

    interface INetFramework46 : INetFramework452, INetStandard13
    {
        // .NET Framework 4.6 specific APIs
    }

    interface INetFramework461 : INetFramework46, INetStandard14
    {
        // .NET Framework 4.6.1 specific APIs
    }

    interface INetFramework462 : INetFramework461, INetStandard15
    {
        // .NET Framework 4.6.2 specific APIs
    }

    // .NET Core
    interface INetCoreApp10 : INetStandard15
    {
        // TODO: .NET Core 1.0 specific APIs
    }
    // Windows Universal Platform
    interface IWindowsUniversalPlatform : INetStandard13
    {
        void GPS();
        void Xaml();
    }

    // Xamarin
    interface IXamarinIOS : INetStandard15
    {
        void AppleAPIs();
    }

    interface IXamarinAndroid : INetStandard15
    {
        void GoogleAPIs();
    }
    // Future platform

    interface ISomeFuturePlatform : INetStandard13
    {
        // A future platform chooses to implement a specific .NET Standard version.
        // All libraries that target that version are instantly compatible with this new
        // platform
    }

}

Source


P
Peter Mortensen

Another way of explaining the difference could be with real world examples, as most of us mere mortals will use existing tools and frameworks (Xamarin, Unity, etc.) to do the job.

So, with .NET Framework you have all the .NET tools to work with, but you can only target Windows applications (UWP, Windows Forms, ASP.NET, etc.). Since .NET Framework is closed source there isn't much to do about it.

With .NET Core you have fewer tools, but you can target the main desktop platforms (Windows, Linux, and Mac). This is specially useful in ASP.NET Core applications, since you can now host ASP.NET on Linux (cheaper hosting prices). Now, since .NET Core was open sourced, it's technically possible to develop libraries for other platforms. But since there aren't frameworks that support it, I don't think that's a good idea.

With .NET Standard you have even fewer tools, but you can target all/most platforms. You can target mobile thanks to Xamarin, and you can even target game consoles thanks to Mono/Unity. It's also possible to target web clients with the UNO platform and Blazor (although both are kind of experimental right now).

In a real-world application you may need to use all of them. For example, I developed a point of sale application that had the following architecture:

Shared both server and slient:

A .NET Standard library that handles the models of my application.

A .NET Standard library that handles the validation of data sent by the clients.

Since it's a .NET Standard library, it can be used in any other project (client and server).

Also a nice advantage of having the validation on a .NET standard library since I can be sure the same validation is applied on the server and the client. Server is mandatory, while client is optional and useful to reduce traffic.

Server side (Web API):

A .NET Standard (could be .NET Core as well) library that handles all the database connections.

A .NET Core project that handles the Rest API and makes use of the database library.

As this is developed in .NET Core, I can host the application on a Linux server.

Client side (MVVM with WPF + Xamarin.Forms Android/iOS):

A .NET Standard library that handles the client API connection.

A .NET Standard library that handles the ViewModels logic. It is used in all the views.

A .NET Framework WPF application that handles the WPF views for a windows application. WPF applications can be .NET core now, although they only work on Windows currently. AvaloniaUI is a good alternative for making desktop GUI applications for other desktop platforms.

A .NET Standard library that handles Xamarin forms views.

A Xamarin Android and Xamarin iOS project.

So you can see that there's a big advantage here on the client side of the application, since I can reuse both .NET Standard libraries (client API and ViewModels) and just make views with no logic for the WPF, Xamarin and iOS applications.


I think this is a better answer since it incorporates the real-world.
2
2 revs, 2 users 67%

.NET Standard: Think of it as a big standard library. When using this as a dependency you can only make libraries (.DLLs), not executables. A library made with .NET standard as a dependency can be added to a Xamarin.Android, a Xamarin.iOS, a .NET Core Windows/OS X/Linux project.

.NET Core: Think of it as the continuation of the old .NET framework, just it's opensource and some stuff is not yet implemented and others got deprecated. It extends the .NET standard with extra functions, but it only runs on desktops. When adding this as a dependency you can make runnable applications on Windows, Linux and OS X. (Although console only for now, no GUIs). So .NET Core = .NET Standard + desktop specific stuff.

Also UWP uses it and the new ASP.NET Core uses it as a dependency too.


P
Peter Mortensen

.NET Standard exists mainly to improve code sharing and make the APIs available in each .NET implementation more consistent.

While creating libraries we can have the target as .NET Standard 2.0 so that the library created would be compatible with different versions of .NET Framework including .NET Core, Mono, etc.


P
Peter Mortensen

The previous answers may describe the best understanding about the difference between .NET Core, .NET Standard and .NET Framework, so I just want to share my experience when choosing this over that.

In the project that you need to mix between .NET Framework, .NET Core and .NET Standard. For example, at the time we build the system with .NET Core 1.0, there is no support for Window Services hosting with .NET Core.

The next reason is we were using Active Report which doesn't support .NET Core.

So we want to build an infrastructure library that can be used for both .NET Core (ASP.NET Core) and Windows Service and Reporting (.NET Framework) -> That's why we chose .NET Standard for this kind of library. Choosing .NET standard means you need to carefully consider every class in the library should be simple and cross .NET (Core, Framework, and Standard).

Conclusion:

.NET Standard for the infrastructure library and shared common. This library can be referenced by .NET Framework and .NET Core.

.NET Framework for unsupported technologies like Active Report, Window Services (now with .NET 3.0 it supports).

.NET Core for ASP.NET Core of course.

Microsoft just announced .NET 5: Introducing .NET 5


@Gigi Please read my answer carefully, I said it was when .NET Core in a 1.0 version and this case we want to design a solution to combine both .NET core and .NET framework. The ASP.NET Core support .NET Framework from 2.0 above. My answer is a story when you have to deal with multiple version of .NET. So I can't understand why you have a downvote when you don't understand the situation correctly.
Thanks for your answer - I did read your answer, and I did read the part where you referred to .NET Core 1.0. Yet I did not take that as a prerequisite to interpreting your conclusions, which would otherwise mislead people reading with the current versioning. Additionally, it seems like my comment was nuked by the Stack Overflow police, which is a shame that I've grown accustomed to on this site.
B
Bilawal Khan

.NET Core .NET Core is a free, cross-platform, open source implementation of the managed framework. It supports four types of applications: console, ASP.NET Core, cloud, and Universal Windows Platform (UWP). Windows Forms and Windows Presentation Foundation(WPF) are not part of .NET Core.

Technically, .NET Core only supports console applications. ASP.NET Core and UWP are application models built on top of .NET Core.

Unlike the .NET Framework, .NET Core is not considered a Windows component. Therefore, updates come as NuGet packages, not through Windows Update. Since the .NET Core runtime is installed App-Local, and applications are updated through the package manager, applications can be associated with a particular .NET Core version and be updated individually.

.NET Standard Each implementation of the managed framework has its own set of Base Class Libraries. The Base Class Library (BCL) contains classes such as exception handling, strings, XML, I/O, networking, and collections.

.NET Standard is a specification for implementing the BCL. Since a .NET implementation is required to follow this standard, application developers will not have to worry about different versions of the BCL for each managed framework implementation.

Framework Class Libraries (FCL) such as WPF, WCF, and ASP.NET are not part of the BCL, and therefore are not included in .NET Standard.

The relationship between .NET Standard and a .NET implementation is the same as between the HTML specification and a browser. The second is an implementation of the first.

Hence, the .NET Framework, Xamarin, and .NET Core each implement .NET Standard for the BCL in their managed framework. Since the computer industry will continue to introduce new hardware and operating systems, there will be new managed frameworks for .NET. This standard allows application developers to know that there will be a consistent set of APIs that they can rely on.

Each .NET version has an associated version of the .NET Standard.

By providing consistent APIs, porting applications to different managed implementations, as well as providing tooling, is easier.

.NET Standard is defined as a single NuGet package because all .NET implementations are required to support it. Tooling becomes easier because the tools have a consistent set of APIs to use for a given version. You can also build a single library project for multiple .NET implementations.

You can also build .NET Standard wrappers for platform specific APIs.


P
Peter Mortensen

.NET Framework

Windows Forms, ASP.NET and WPF application must be developed using .NET Framework library.

.NET Standard

Xamarin, iOS and Mac OS X application must be developed using .NET Standard library

.NET Core

Universal Windows Platform (UWP) and Linux application must be developed using .NET Core library. The API is implemented in C++ and you can using the C++, VB.NET, C#, F# and JavaScript languages.NET


N
Naveed Hematmal

Every Framework has its class library.

Base Class Library for .Net Framework.

Core Library for .Net core.

Mono Class Library for Xamarin.

Microsoft has decided to bring all these class libraries to a single library that is implementable in all frameworks. For this purpose, they developed .Net standard.

Microsoft has decided to make a unified Framework. .Net 5 is a unified framework of .Net core and .Net Framework. In .Net 6, they merge Xamarin under the .Net MAUI project with .Net as well.

.Net Framework, .Net Core, Xamarin are unified to a single Framework .Net 6, so don't need to .Net standard. The goal of the .Net standard was to have a library that works in all frameworks. Now all frameworks are merged in .Net 6.