理解 .NET Platform Standard

来源:http://www.cnblogs.com/xishuai/archive/2016/05/24/understand-dotnet-platform-standard.html
-Advertisement-
Play Games

相關博文: "ASP.NET 5 Target framework dnx451 and dnxcore50" .NET Platform Standard:https://github.com/dotnet/corefx/blob/master/Documentation/architecture ...


相關博文:ASP.NET 5 Target framework dnx451 and dnxcore50

.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 列表:

Target Platform Name Alias
.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配置了net451netstandard1.3netcore50,這些是什麼意思?從上面的 .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配置了net451netstandard1.3netcore50,也就是說Microsoft.EntityFrameworkCore可以被這三種平臺的程式集引用,比如你的程式集frameworks中只配置了net451,照樣可以引用Microsoft.EntityFrameworkCore程式集,只不過只能在 Windows 上運行,不能跨平臺而已,一個程式集不同平臺的代碼寫法:

#if DNX451
    //Code here for dnx451
#elif DNXCORE50
    //code here for dnxcore50
#endif

imports的解釋是(來自 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 系統。

Platform NuGet identifier
.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.Interfacesproject.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.Infrastructureproject.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 的程式包。

參考資料:


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 三大範式一直沒有記住,看了這個有了理解!挺好的記著,以後忘了,可以再看看! 為了建立冗餘較小、結構合理的資料庫,設計資料庫時必須遵循一定的規則。在關係型資料庫中這種規則就稱為範式。範式是符合某一種設計要求的總結。要想設計一個結構合理的關係型資料庫,必須滿足一定的範式。 在實際開發中最為常見的設計範式 ...
  • 環境:Vs2103(TFS2013) 目的:去掉別人項目里的TFS控制,因為每次打開時會有提示信息 解決方法: 1.刪除隱藏的.$tf文件夾,搜索*.vssscc和*.vspscc這兩個尾碼的文件,刪除找到的文件. 2.使用文本編輯器打開*.sln文件,找到 GlobalSection(TeamFo ...
  • 剛到新單位,學習他們的源代碼,代碼里讀寫系統配置文件的XML代碼比較老套,直接寫在一個系統配置類里,沒有進行類的拆分,造成類很龐大,同時,操作XML的讀寫操作都是使用SetAttribute和node.Attribute(name)方法,因此,想到結合之前所做的XML操作,完成了一個能夠讀取XML文 ...
  • 寫入: 插入100萬條數據:用InsertMany,耗時16s左右。 讀取: 讀取300萬條數據,耗時3600毫秒。 ...
  • ...
  • 這次要分享的是C#Task任務的幾個列子,感覺最實用的是封裝的分頁任務執行方法,這個方法步奏也是目前在我工作中執行多任務常用的,不知道各位也有這用的情況,那麼開始吧。 1.順序任務執行 1 //順序任務執行 2 Task.Factory.StartNew<int>(() => { Console.W ...
  • 關於面試中涉及到的事件的問題,我們只需要抓住幾個關鍵點就好了: 定義事件: 5 } ...
  • 上接 WCF學習之旅—WCF服務部署到IIS7.5(九) WCF學習之旅—WCF服務部署到應用程式(十) 七 WCF服務的Windows 服務程式寄宿 這種方式的服務寄宿,和IIS一樣有一個一樣的優點,系統啟動後,WCF服務也會跟著啟動了,不用人工干預,也是一種較好的寄宿方式。 (1) 在解決方案下 ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...