In this post, I outline some bits I have learned about .NET standard.
.NET standard is a contract which defines the set of APIs (namespaces, classes and methods) you can use for working with file system, networking, threading, etc … I think of it as similar to an interface, but at a much larger scale. As the name suggests, .NET standard defines the APIs, but it does not contain implementations. The actual frameworks that implement .NET Standard include .NET framework, .NET core, Mono, and Xamarin.
.NET Standard is a specification of .NET APIs that are available on all .NET implementations. Targeting .NET Standard lets you produce libraries that are constrained to use APIs that are in a given version of .NET Standard, which means it’s usable by all platforms that implement that version of the .NET Standard.
Cross-Platform Targeting
.NET framework has been around for a long time, and it has powered billions of systems. The full .NET framework only runs on Windows and contains a heavy set of libraries, many of which you may not need in a project. .NET Core is like a cool kid in town. It is cross-platform, lightweight and more performant compared to the full .NET framework. However, .NET Core does not contain all the libraries that exist in .NET framework, especially the ones that requires Windows. While Microsoft is putting more emphasis and support on .NET Core, .NET Framework is here to stay because too many things depend on it. What does it mean for us as developers? If you want to build a library and want it to be usable by different projects that target different .NET frameworks, you need to target a version of .NET Standard which all the frameworks you want to support implement. For instance, If you library targets just a version of the full .NET framework, it is not going to be usable by a project that targets .NET Core because your library could use APIs that are not available in .NET Core and vice versa.
The lowest version of .NET Standard is 1.0 and the latest version, as of this writing is 2.1, which is still in preview. Each implementation of .NET Standard implements a specific version of it and all the versions below it. For instance, .NET core 2.0 implements .NET Standard 2.0, which means it implements .NET Standard versions from 1.0 up to and including 2.0 (1.0, 1.1, …1.6, 2.0). The lower the version of .NET Standard you target, the less APIs you have available to you, and the broader .NET applications can consume your library.
The table below helps to determine which version of .NET Standard you should target. It is from the documentation.
.NET Standard | 1.0 | 1.1 | 1.2 | 1.3 | 1.4 | 1.5 | 1.6 | 2.0 | 2.1 Preview |
---|---|---|---|---|---|---|---|---|---|
.NET Core | 1.0 | 1.0 | 1.0 | 1.0 | 1.0 | 1.0 | 1.0 | 2.0 | 3.0 |
.NET Framework 1 | 4.5 | 4.5 | 4.5.1 | 4.6 | 4.6.1 | 4.6.1 2 | 4.6.1 2 | 4.6.1 2 | N/A3 |
Mono | 4.6 | 4.6 | 4.6 | 4.6 | 4.6 | 4.6 | 4.6 | 5.4 | 6.4 |
Xamarin.iOS | 10.0 | 10.0 | 10.0 | 10.0 | 10.0 | 10.0 | 10.0 | 10.14 | 12.16 |
Xamarin.Mac | 3.0 | 3.0 | 3.0 | 3.0 | 3.0 | 3.0 | 3.0 | 3.8 | 5.16 |
Xamarin.Android | 7.0 | 7.0 | 7.0 | 7.0 | 7.0 | 7.0 | 7.0 | 8.0 | 10.0 |
Universal Windows Platform | 10.0 | 10.0 | 10.0 | 10.0 | 10.0 | 10.0.16299 | 10.0.16299 | 10.0.16299 | TBD |
Unity | 2018.1 | 2018.1 | 2018.1 | 2018.1 | 2018.1 | 2018.1 | 2018.1 | 2018.1 | TBD |
The following steps show how to use the table to select a version of .NET Standard for your project:
For example, suppose you build a library and want to make available to two other projects that target .NET Core 1.0 and .NET framework 4.5.1 respectively. Looking at the table, you see the highest .NET Standard version which .NET Core 1.0 implements is 1.6. However, the highest .NET Standard version which .NET framework 4.5.1 implements is 1.2. Therefore, you should target .NET Standard 1.2 if you want your library to be usable by both the two projects.
Other tips to help with selecting an appropriate .NET Standard version:
These urls are from the video on “Build great libraries using .NET Standard”. The video contains great info and tips for working with .NET Standard and targeting multiple frameworks. You can find the link in the References section.
Can a .NET Core Web app consume .NET Framework class libraries?
Enhancing ASP.NET Core/Blazor App Security and Reusability with HttpMessageHandler and Named HttpClient
Building a fully multitenant system using Microsoft Identity Framework and SQL Row Level Security
Analyzing a rental property through automation
Web scraping in C# using HtmlAgilityPack
Building multitenant application – Part 2: Storing value into database session context from ASP.NET core web API
Build and deploy a WebJob alongside web app using azure pipelines
Authenticate against azure ad using certificate in a client credentials flow
Notes on The Clean Architecture