理解 .NET Platform Standard
.NET Platform Standard:https://github.com/dotnet/corefx/blob/master/Documentation/architecture/net-platform-standard.md
.NET Platform Standard 是什么?直譯過來就是 .NET 平臺規范或標準,它的目的就是使 .NET 各個平臺之間更加統一和規范,在之前的 .NET Core RC2 發布文章中提到了 .NET Standard Library,它其實就是 .NET Platform Standard 的體現之一,.NET Standard Library 現在有一個對應程序包NETStandard.Library,它的作用是兼容各個 .NET Platform,這個后面有進行說明,現在只是一個臨時方案,以后微軟慢慢會把相關的程序包(比如基礎類庫等),按照 .NET Standard Library 的標準進行開發和發布。
.NET Platform Standard 列表:
| .NET Platform Standard | netstandard | 1.0 | 1.1 | 1.2 | 1.3 | 1.4 | 1.5 | 1.6 |
| .NET Core | netcoreapp | → | → | → | → | → | → | 1.0 |
| .NET Framework | net | → | → | → | → | → | → | 4.6.3 |
| → | → | → | → | → | 4.6.2 | |||
| → | → | → | → | 4.6.1 | ||||
| → | → | → | 4.6 | |||||
| → | → | 4.5.2 | ||||||
| → | → | 4.5.1 | ||||||
| → | 4.5 | |||||||
| Universal Windows Platform | uap | → | → | → | → | 10.0 | ||
| Windows | win | → | → | 8.1 | ||||
| → | 8.0 | |||||||
| Windows Phone | wpa | → | → | 8.1 | ||||
| Windows Phone Silverlight | wp | 8.1 | ||||||
| 8.0 | ||||||||
| Mono/Xamarin Platforms | → | → | → | → | → | → | * | |
| Mono | → | → | * |
上面這些都是概念,我們在 ASP.NET Core 1.0 RC2 項目開發中,如何應用和體現呢?其實就是我們在project.json中配置的frameworks節點,我們先看一段配置(來自 Microsoft.EntityFrameworkCore/project.json):
"frameworks": {"net451": {"frameworkAssemblies": {"System.ComponentModel.DataAnnotations": "","System.Runtime": {"type": "build"}}},"netstandard1.3": {"imports": ["portable-net452+win81"],"dependencies": {"System.Collections.Concurrent": "4.0.12-*","System.ComponentModel.Annotations": "4.1.0-*","System.Linq.Queryable": "4.0.1-*","System.ObjectModel": "4.0.12-*","System.Reflection.Extensions": "4.0.1-*","System.Reflection.TypeExtensions": "4.1.0-*"}},"netcore50": {"dependencies": {"Microsoft.NETCore.Platforms": {"type": "build","version": "1.0.1-*"},"System.Collections.Concurrent": "4.0.10","System.ComponentModel.Annotations": "4.0.10","System.Linq.Queryable": "4.0.0","System.ObjectModel": "4.0.10","System.Reflection.Extensions": "4.0.0","System.Reflection.TypeExtensions": "4.0.0","System.Runtime": {"type": "build","version": "4.0.20"},"System.Dynamic.Runtime": {"type": "build","version": "4.0.10"},"System.Runtime.WindowsRuntime": {"type": "build","version": "4.0.10"},"System.Runtime.Extensions": {"type": "build","version": "4.0.10"}}}}可以看到frameworks配置了net451、netstandard1.3和netcore50,這些是什么意思?從上面的 .NET Platform Standard 列表中,我們可以得到一些信息,但還是有些不太明白,我們看一下相關解釋(來自 Project.json definition dnx451 vs .dotnet (4.51)):
dnxcore50: DNX SDK running on CoreCLR/CoreFx (deprecated, use netcoreapp1.0 instead).
dnx451: DNX SDK running on .Net 4.5.1 (Desktop CLR / Full BCL and FCL) (deprecated, use net451 instead).
net46: .Net Framework 4.6 SDK running on Desktop CLR / Full BCL and FCL.
uap10.0: UWP Windows 10 SDK running on .Net Native/CoreFx.
netcoreapp1.0: .NET Core 1.0 SDK running on CoreCLR/CoreFx.
netstandard1.5: (RC2, dotnet before) any pure IL code which declares its dependencies (System.Runtime (based) libraries instead of a PCL contracts). Framework dependencies are available for .Net 4.5.x onwards, .NET Core or UWP (System.Runtime based library set in different versions). As with RC2 dotnet is deprecated, use netstandard instead.
先看dnxcore50的解釋,DNX SDK 是什么?它其實是一種命令或工具,指向你的程序集使用的哪種目標環境,CoreCLR/CoreFx 就是說,程序集跑在 CoreCLR/CoreFx 上,dnxcore50現在已經被棄用了,被 netcoreapp1.0所替代,netstandard1.5是一種新的平臺規范,使用它必須引用NETStandard.Library程序包,否則System所有相關命名空間都找不到。
簡單來說,frameworks所配置的就是你程序集的運行環境或平臺,如果配置了多個,就表示程序集可以跑在多個平臺上,比如,上面Microsoft.EntityFrameworkCore配置了net451、netstandard1.3和netcore50,也就是說Microsoft.EntityFrameworkCore可以被這三種平臺的程序集引用,比如你的程序集frameworks中只配置了net451,照樣可以引用Microsoft.EntityFrameworkCore程序集,只不過只能在 Windows 上運行,不能跨平臺而已,一個程序集不同平臺的代碼寫法:
//Code here for dnx451//code here for dnxcore50imports的解釋是(來自 Frameworks and imports sections in project.json: what are they?):imports is a way to use packages that were not designed for that framework. Basically you tell it "Use those targets even though they don't seem to be supported. I know what I'm doing". 簡單來說,就是兼容本程序集配置平臺所不支持的平臺,有點繞,我們做一個測試就清楚了:
如上圖的配置,為什么frameworks配置了netcoreapp1.0會出現錯誤?因為我們引用的Microsoft.EntityFrameworkCore程序包并不完全支持netcoreapp1.0平臺,所以我們需要在netcoreapp1.0下增加"imports": ["net451"]配置,其作用就是使之兼容,當然現在只是兼容平臺的配置,以后完善之后,這個配置會去掉的。
imports 的一段配置:
"netcoreapp1.0": {"imports": ["net461","portable-net45+win81"]}首先,imports 可以配置多個節點,portable-net45+win81是什么意思?portable 的意思是便攜式的,在之前的博文中有提及,意思就是自身發布不攜帶依賴的程序包,而是使用系統中安裝配置的,net45就是上面說的frameworks配置,win81是系統平臺的意思,但不只是特指 Windows 8.1 系統。
| .NET Framework 2.0 - 4.6 | net20 - net46 |
| .NET Core | netcoreapp |
| .NET Micro Framework | netmf |
| Windows 8 | win8, netcore45 |
| Windows 8.1 | win8, netcore451 |
| Windows Phone Silverlight (8, 8.1) | wp8, wp81 |
| Windows Phone 8.1 | wpa8.1 |
| Universal Windows Platform 10 | uap10, netcore50 |
| Silverlight 4, 5 | sl4, sl5 |
| MonoAndroid | monoandroid |
| MonoTouch | monotouch |
| MonoMac | monomac |
| Xamarin iOS | xamarinios |
| Xamarin PlayStation 3 | xamarinpsthree |
| Xamarin PlayStation 4 | xamarinpsfour |
| Xamarin PlayStation Vita | xamarinpsvita |
| Xamarin Watch OS | xamarinwatchos |
| Xamarin TV OS | xamarintvos |
| Xamarin Xbox 360 | xamarinxboxthreesixty |
| Xamarin Xbox One | xamarinxboxone |
最后,再做一個測試,這個我們一般在 ASP.NET 5 Core 1.0 RC1 升級到 RC2 中會遇到的,兩個程序集:
CNBlogs.Ad.Infrastructure.Interfaces
CNBlogs.Ad.Infrastructure: 依賴于CNBlogs.Ad.Infrastructure.Interfaces
CNBlogs.Ad.Infrastructure.Interfaces的project.json配置:
{"version": "1.0.0-*","description": "CNBlogs.Ad.Infrastructure.Interfaces Class Library","authors": [ "xishuai" ],"frameworks": {"netcoreapp1.0": {"imports": ["net451"]},"net451": { }},"dependencies": {"Microsoft.EntityFrameworkCore": "1.0.0-rc2-final","Microsoft.EntityFrameworkCore.SqlServer": "1.0.0-rc2-final"}}CNBlogs.Ad.Infrastructure的project.json配置:
{"version": "1.0.0-*","description": "CNBlogs.Ad.Infrastructure Class Library","authors": [ "xishuai" ],"frameworks": {"netcoreapp1.0": {"imports": ["net451"]},"net451": { }},"dependencies": {"CNBlogs.Ad.Infrastructure.Interfaces": "1.0.0-*","Microsoft.EntityFrameworkCore": "1.0.0-rc2-final","Microsoft.EntityFrameworkCore.SqlServer": "1.0.0-rc2-final"}}幾種測試情況:
CNBlogs.Ad.Infrastructure.Interfaces中的netcoreapp1.0去除"imports": ["net451"]配置:出現錯誤,上面有過分析,因為Microsoft.EntityFrameworkCore并不完全支持netcoreapp1.0。
CNBlogs.Ad.Infrastructure.Interfaces去除netcoreapp1.0配置:出現錯誤,因為CNBlogs.Ad.Infrastructure配置了netcoreapp1.0,而引用的CNBlogs.Ad.Infrastructure.Interfaces卻支持net451。
CNBlogs.Ad.Infrastructure.Interfaces去除net451配置:出現錯誤,同上,因為CNBlogs.Ad.Infrastructure配置了net451,而引用的CNBlogs.Ad.Infrastructure.Interfaces卻支持netcoreapp1.0。
CNBlogs.Ad.Infrastructure去除netcoreapp1.0配置:成功,因為依賴的CNBlogs.Ad.Infrastructure支持net451。
CNBlogs.Ad.Infrastructure去除net451配置:出現成功,同上,因為依賴的CNBlogs.Ad.Infrastructure支持netcoreapp1.0。
綜合上面的測試,簡單來說,就是程序包的運行平臺或環境取決于底層的引用,底層的引用指的是你自己項目中的程序包,而不是基礎類庫和微軟開發的程序包,因為它們都支持多平臺,比如上面的Microsoft.EntityFrameworkCore程序包。
另外,如果你的程序包frameworks配置的是net451,它其實和 .NET Core 沒多大關系了,因為它使用的是 .NET Framework 和 Desktop CLR,而不是 CoreCLR 和 CoreFx,即使你項目中使用的是 .NET Core RC2 的程序包。
相關文章:
ASP.NET Core 1.0 入門——了解一個空項目
ASP.NET Core 1.0 部署 HTTPS (.NET Framework 4.5.1)
.NET Core 1.0、ASP.NET Core 1.0和EF Core 1.0簡介
云服務器下ASP.NET Core 1.0環境搭建(包含mono與coreclr)
使用VS Code開發ASP.NET Core 應用程序
dotnet run是如何啟動asp.net core站點的
ASP.NET Core提供模塊化Middleware組件
“dotnet restore"和"dotnet run"都做了些什么?
探秘 dotnet run 如何運行 .NET Core 應用程序
.NET Portability Analyzer 已開源
ASP.NET Core的配置(1):讀取配置信息
ASP.NET Core的配置(2):配置模型詳解
.NET Core 1.0 RC2 歷險之旅
使用VS Code開發 調試.NET Core 應用程序
讓我們Core在一起:ASP.NET Core & .NET Core
.NET Core VS Code 環境配置
官方博客明確了 .NET Core RC2/RTM 時間表
.NET Core全新的配置管理[共9篇]
利用記事本創建一個ASP.NET Core RC2 MVC應用
微軟.NET 正式劈腿成功,橫跨所有平臺
.NET Core 1.0 CentOS7 嘗試
解讀發布:.NET Core RC2 and .NET Core SDK Preview 1
[.NET Core].NET Core R2安裝及示例教程
升級ASP.Net Core項目
升級.Net Core RC1的類庫項目
Entity Framework升級
TFS2015的CI集成
結合Jexus + Kestrel 部署 asp.net core 生產環境
.NET Core計劃棄用project.json
原文地址:http://www.cnblogs.com/xishuai/p/understand-dotnet-platform-standard.html
.NET社區新聞,深度好文,微信中搜索dotNET跨平臺或掃描二維碼關注
總結
以上是生活随笔為你收集整理的理解 .NET Platform Standard的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IIS负载均衡-Application
- 下一篇: 搜狐视频Redis私有云cacheclo