- Modern Web Development with ASP.NET Core 3
- Ricardo Peres
- 647字
- 2021-06-18 18:35:52
Dependencies and frameworks
Inside a .NET Core project, you specify the frameworks that you wish to target. What are these frameworks? Well, .NET Core itself, but the classic .NET Framework as well, Xamarin, Universal Windows Platform (UWP), Portable Class Libraries (PCL), Mono, Windows Phone, and more.
In the early days of .NET Core, you would either target .NET Core itself, or/as well as one of these other frameworks. Now it is advisable to target standards instead. Now we have .NET Standard, and the differences between the two are as follows:
- .NET Standard is a specification (a contract) that covers which APIs a .NET platform has to implement.
- .NET Core is a concrete .NET platform and implements the .NET Standard.
- The latest .NET Standard will always cover the highest .NET full framework released.
David Fowler (https://twitter.com/davidfowl) of Microsoft came up with the following analogy:
interface INetStandard10
{
void Primitives();
void Reflection();
void Tasks();
void Collections();
void Linq();
}
interface INetStandard11 : INetStandard10
{
void ConcurrentCollections();
void InteropServices();
}
interface INetFramework45 : INetStandard11
{
// Platform specific APIs
void AppDomain();
void Xml();
void Drawing();
void SystemWeb();
void WPF();
void WindowsForms();
void WCF();
}
This should make it very easy to understand. As you can see, all .NET APIs that need Windows (WPF, Windows Forms, Drawing) are only available in a specific platform (.NET 4.5), not a standard. Standards are for cross-platform functionality.
So instead of targeting a specific version, such as .NET 4.5.1, .NET Core 1.0, Mono, Universal Windows Platform 10, you should target a .NET Standard. Your project is guaranteed to work on all platforms that support that standard (or a higher one), either existing or waiting to be created. You should try to keep your dependency to the lowest standard possible to increase the number of platforms that your app will work on, if that is important to you.
The current mapping between the different .NET frameworks and the .NET Standard they implement at the time this book was written is always available at https://github.com/dotnet/standard/blob/master/docs/versions.md.
.NET Core 2.0 and .NET Standard 2.0 were made available in August 2017, and now four frameworks target .NET Standard 2.0:
- .NET Framework full
- .NET Core 2.x
- Xamarin
- Mono
.NET Core 3.0 was made available on September 2019 and with it .NET Standard 2.1.
You can have your dependencies specified per target or for all targets. In the former case, all of the dependencies need to support all of the targets, and in the latter, we can have different dependencies for each target. You'll probably want a mix of the two, with common libraries as global dependencies and more specialized libraries specified only where available. If you target more than one standard (or framework), then pay attention, because you may have to resort to conditional definitions (#if) to target those features that only exist in one of them. Let's see how.
Targeting .NET Core or the full .NET framework
It is important that you know that you can target the full .NET framework in an ASP.NET Core application! However, if you do this, you will lose the platform independence—that is, you will only be able to run it on Windows.
By default, an ASP.NET Core project targets netcoreapp1.x, netcoreapp2.x, or netcoreapp3.x, depending on whether you are targeting ASP.NET Core 1.x, 2.x, or 3.x, but you can change it in the .csproj file. If you just want to target one framework, then modify the TargetFramework element like this:
<TargetFramework>net461</TargetFramework>
Or, if you want to target more than one, replace TargetFrameworkwith TargetFrameworks :
<TargetFrameworks>netcoreapp3.0;net461</TargetFrameworks>
For more information, please refer to the Microsoft documentation at https://docs.microsoft.com/en-us/dotnet/core/tools/csproj.
For .NET Core and .NET Standard, you should use the following names in TargetFramework or TargetFrameworks:

Please see https://docs.microsoft.com/en-us/dotnet/standard/frameworks for the up-to-date list. Next, let's see how generic hosting works.
- Learning Real-time Processing with Spark Streaming
- oreilly精品圖書:軟件開發者路線圖叢書(共8冊)
- Bootstrap Essentials
- 零基礎學Java程序設計
- Developing SSRS Reports for Dynamics AX
- ExtJS Web應用程序開發指南第2版
- Java并發編程之美
- Getting Started with Polymer
- 創意UI Photoshop玩轉移動UI設計
- WildFly Cookbook
- Mastering VMware Horizon 7(Second Edition)
- 你好!Java
- Building Clouds with Windows Azure Pack
- Getting Started with RethinkDB
- C語言解惑:指針、數組、函數和多文件編程