日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

魅力 .NET:从 Mono、.NET Core 说起

發(fā)布時(shí)間:2023/12/18 asp.net 55 豆豆
生活随笔 收集整理的這篇文章主要介紹了 魅力 .NET:从 Mono、.NET Core 说起 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

轉(zhuǎn)自:http://kb.cnblogs.com/page/514268/

前段時(shí)間,被問了這樣一個(gè)問題:.NET 應(yīng)用程序是怎么運(yùn)行的?

  當(dāng)時(shí)大概愣了好久,好像也沒說出個(gè)所以然,得到的回復(fù)是:這是 .NET 程序員最基本的。。。呵呵!

  微軟開源,其實(shí)不只是對 .NET 本身有利,從另一方面講,對于 .NET 程序員來說,因?yàn)殚_源,你可以想了解到你想要的任何事。在之前的歲月,你可以“平凡”做一個(gè)默默無聞的 C# 代碼撰寫者,可以不用考慮任何事,使用宇宙最強(qiáng)大的 IDE - Visual Studio 編寫代碼后,發(fā)布到 IIS 即可,就是這么簡單,甚至你不需要知道 IIS 是怎么運(yùn)行的,代碼是怎么托管的等等,你只需要寫好你的 C# 代碼即可,因?yàn)槠溆嗟囊磺泄ぷ?#xff0c;微軟已經(jīng)幫你考慮并完成了。這樣雖然簡單并且高效,但對于 .NET 程序員來說,這就像“井底之蛙”一樣,你看不到外面的世界,你也沒有任何動(dòng)力去跳出這個(gè)“深井”,而只是“舒適”的觀望著外面你所認(rèn)為的“狹隘世界”。但因?yàn)殚_源、跨平臺(tái),這些平靜的日子都將被打破了,微軟開源出來的一個(gè)個(gè)項(xiàng)目、發(fā)表的一篇篇文章,怎么越來越看不懂了?最后才發(fā)現(xiàn),現(xiàn)在的微軟已經(jīng)不是原來我們熟悉的那個(gè)微軟了,.NET 亦是如此。

  我大概花了幾天的時(shí)間,去真正閱讀 .NET 開源的一些文章,雖然英文很爛,但我都堅(jiān)持讀了下來,閱讀過程中,越來越被 .NET 的魅力所吸引,并時(shí)不時(shí)的被一些“特殊表達(dá)“所逗笑,但這都只是在閱讀的過程中,讀完、思考完,回到上面的那個(gè)問題,我還是不能很好的去表述、回答它,這時(shí)候我就意識(shí)到:腦子不好使,需要博客記錄,這篇博文可以當(dāng)做一篇閱讀筆記來看(再一次的閱讀)。

  在很久很久之前,如果提到 .NET 跨平臺(tái),你首先想到的應(yīng)該是?Mono,那 Mono 到底是什么?它為什么可以跨平臺(tái)?在 .NET 開源之前,我對這些是一無所知,理解微軟這次 .NET 跨平臺(tái),需要首先了解 Mono,了解 Mono,首先你需要仔細(xì)閱讀下這篇文章:http://www.wikipedia.org/wiki/Mono。

  摘錄

  Mono 是一個(gè)由 Xamarin 公司(先前是 Novell,最早為 Ximian)所主持的自由開放源代碼項(xiàng)目。該項(xiàng)目的目標(biāo)是創(chuàng)建一系列符合 ECMA 標(biāo)準(zhǔn)(Ecma-334 和 Ecma-335)的 .NET 工具,包括?C# 編譯器通用語言架構(gòu)。與微軟的 .NET Framework(共通語言運(yùn)行平臺(tái))不同,Mono 項(xiàng)目不僅可以運(yùn)行于 Windows 系統(tǒng)上,還可以運(yùn)行于 Linux,FreeBSD,Unix,OS X 和 Solaris,甚至一些游戲平臺(tái),例如:Playstation 3,Wii 或 XBox 360。

  重要的兩點(diǎn):

  • C# 編譯器:Mono 的 C# 編譯器及其相關(guān)工具發(fā)布于?GNU 通用公共許可證(GPL)之下,其運(yùn)行時(shí)庫發(fā)布于?GNU 寬通用公共許可證(LGPL)之下,其類庫發(fā)布于?MIT 許可證之下。這些均是開源協(xié)議,因此 Mono 是一個(gè)開源軟件。
  • 通用語言架構(gòu):微軟開發(fā)了一個(gè)稱為通用語言架構(gòu)(Shared Source Common Language Infrastructure,Shared Source CLI;即今 ECMA—通用語言架構(gòu))的可用于 FreeBSD,Windows 和 Mac OS X 的 .NET 實(shí)現(xiàn)版本。微軟的共享源代碼協(xié)議并不是開源軟件協(xié)議,且可能對于社區(qū)來說也是不足夠的(它明文禁止了對軟件的商業(yè)用途)。

  再來看兩個(gè)概念:

  • 公共語言基礎(chǔ)(Common Language Infrastructure,CLI)是一套標(biāo)準(zhǔn)(ECMA335),公共語言運(yùn)行時(shí)(Common Language Runtime)即 CLR 是 CLI 標(biāo)準(zhǔn)的實(shí)現(xiàn),Mono 是實(shí)現(xiàn)者之一。該運(yùn)行時(shí)用于執(zhí)行已編譯的 .NET 應(yīng)用程序。公共語言基礎(chǔ)已被 ECMA 定義為標(biāo)準(zhǔn) ECMA-335。要運(yùn)行一個(gè) .NET 應(yīng)用程序,你必須使用相應(yīng)的參數(shù)調(diào)用運(yùn)行時(shí)。通用語言基礎(chǔ)架構(gòu)定義了構(gòu)成 .NET Framework 基礎(chǔ)結(jié)構(gòu)的可執(zhí)行碼以及代碼的運(yùn)行時(shí)環(huán)境的規(guī)范,它定義了一個(gè)語言無關(guān)的跨體系結(jié)構(gòu)的運(yùn)行環(huán)境,這使得開發(fā)者可以用規(guī)范內(nèi)定義的各種高級(jí)語言來開發(fā)軟件,并且無需修正即可將軟件運(yùn)行在不同的計(jì)算機(jī)體系結(jié)構(gòu)上。

  • 公共語言規(guī)范(Common Language Specification,CLS)定義了提供給公共語言基礎(chǔ)的接口,例如對于枚舉類型的隱含表示類型的協(xié)定。Mono 的編譯器負(fù)責(zé)生成符合公共語言規(guī)范的映射代碼,即公共中間語言(Common Intermediate Language,CIL)。Mono 的運(yùn)行時(shí)將運(yùn)行這類代碼。ECMA 標(biāo)準(zhǔn)先前還定義了一個(gè)符合公共語言規(guī)范的程序庫作為應(yīng)用框架。

  理解

  對于上面一大堆的協(xié)議或概念,理解起來是需要花點(diǎn)時(shí)間,首先 Mono 的各個(gè)部分都是基于各種開源協(xié)議,也就是說它是一個(gè)徹徹底底的開源項(xiàng)目,再來大致理一下協(xié)議及其實(shí)現(xiàn):

  • GPLLGPL?協(xié)議: C# 編譯器及其相關(guān)工具實(shí)現(xiàn)。
  • CLI(公共語言基礎(chǔ)):MS CLR、Mono(CLR 虛擬機(jī)),CLR 全稱為 Common Language Runtime(通用語言運(yùn)行時(shí)),需要注意的是,CLR 和 .NET Framework(.NET 框架)完全不是一個(gè)概念,.NET Framework 是以一種采用系統(tǒng)虛擬機(jī)運(yùn)行的編程平臺(tái),以 CLR 為基礎(chǔ),支持多種語言(C#、VB.NET、C++、Python等)的開發(fā)庫。
  • CLS(公共語言規(guī)范):MSIL(微軟中間語言),現(xiàn)已更改為 CIL,也就是通過編譯器生成的中間語言。

  可以用來回答博文一開始那個(gè)問題的一張圖:

  開發(fā)人員使用高級(jí)編程語言撰寫程序,接下來編譯器將代碼編譯成微軟的中間語言(MSIL),運(yùn)行的時(shí)候 CLR 會(huì)將 MSIL 代碼轉(zhuǎn)換為操作系統(tǒng)的原生代碼(Native code),CLR 內(nèi)置有實(shí)時(shí)編譯器(中間代碼轉(zhuǎn)化為原生代碼的編譯器)。

  是不是還是有點(diǎn)暈?其實(shí)說簡單也簡單,拋開應(yīng)用程序和編譯器,.NET 的運(yùn)行機(jī)制組成,其實(shí)就只有 CIL 和 CLR,CIL 是編譯器生成的中間語言,CLR 的作用就是將它再編譯成原始機(jī)器代碼。你可能還有一個(gè)疑問,我們常說的 .NET Framework 到底是什么?它在 .NET 運(yùn)行機(jī)制中到底扮演什么角色?在上面演示圖中,其實(shí) Source Code 并不包含 .NET Framework,Source Code 指的是你應(yīng)用程序的源代碼,在上面有提到,.NET Framework 是以 CLR 為基礎(chǔ)的開發(fā)庫,你可以把它看作是編譯好的 CIL,它并沒有通過編譯器的再次編譯過程,而是直接通過 CLR 中的編譯器編譯為原始機(jī)器代碼。

  簡要總結(jié):

  • CLI -> CLR
  • CLS -> CIL
  • CLR 運(yùn)行 CIL

  好了,我們再猜想一下,如果把 .NET 運(yùn)行機(jī)制的各個(gè)組件進(jìn)行分開重組,除去 .NET Framework 是微軟提供的,其余的組件,比如 C# 編譯器、CIL、CLR 等,都是 Mono 基于各種協(xié)議進(jìn)行實(shí)現(xiàn)的組件,那如果你的應(yīng)用程序代碼引用了 .NET Framework(微軟實(shí)現(xiàn)),很顯然,這個(gè)應(yīng)用程序是無法運(yùn)行的,因?yàn)閷?shí)現(xiàn)的 CLR 并不是微軟的 CLR,所以針對 .NET Framework 來說,因?yàn)樗牟婚_源,所以 Mono 需要另行實(shí)現(xiàn)基于自己 CLR 的基礎(chǔ)類庫,但這是一個(gè)龐大而復(fù)雜的工程,所以現(xiàn)在的 Mono 基礎(chǔ)類庫并不是很完整,很多東西和微軟的 .NET Framework 并不能很好的兼容,所以你如果使用 Visual Studio 開發(fā)的項(xiàng)目,想遷移到 Mono Develop 下,是非常復(fù)雜的,當(dāng)然,如果你沒有使用任何的 .NET Framework,是非常簡單的,但現(xiàn)實(shí)是,敲個(gè)最簡單的“Hello World”輸出代碼,都得引用 mscorlib.dll 程序集(System.Console)。

  上面描述那么多,我想表達(dá)什么呢?其實(shí)我們現(xiàn)在在 Windows 平臺(tái)下開發(fā)的 .NET 應(yīng)用程序,是深深依賴于 .NET Framework(深深的那種),你的應(yīng)用程序代碼基本上都是在它之上完成的,而 .NET Framework 又是深深依賴于 Windows 平臺(tái)下的 CLR(也是深深的那種),在這種情況下,根本就無法使你的應(yīng)用程序跨平臺(tái),因?yàn)槲④浘o緊的抱住 Windows 平臺(tái),妄想 Windows 可以實(shí)現(xiàn)“大一統(tǒng)”,但現(xiàn)實(shí)是很殘酷的,這次的 .NET 開源、跨平臺(tái),其實(shí)也是微軟的無奈之舉。但就是在這種背景下,Mono 出現(xiàn)了,并且在微軟的各種“排擠”下堅(jiān)持了下來,這是非常不容易的,其實(shí)實(shí)現(xiàn) .NET 跨平臺(tái)的三個(gè)關(guān)鍵是:編譯器CLR?和基礎(chǔ)類庫,而 Mono 實(shí)質(zhì)上就是把他們?nèi)齻€(gè)進(jìn)行跨平臺(tái)實(shí)現(xiàn)了,一個(gè)很小團(tuán)隊(duì)完成了一個(gè)巨頭需要完成的工作,而且還是在這個(gè)巨頭的“排擠”下,其實(shí)這就是開源社區(qū)的力量。

  Mono 就說到這,有些疑問先放下,后面再進(jìn)行講解,下面我們來說一下 .NET Core 的相關(guān)內(nèi)容。

  需要認(rèn)真閱讀的一篇文章:.NET Core is Open Source

  這是 .NET 開源的一篇官方說明文章,內(nèi)容很多(包括評(píng)論),如果你真正花時(shí)間去讀的話,這篇文章的信息量是非常大的,下面是我搜刮的一些片段。

  .NET Core 開源地址:https://github.com/dotnet/corefx。

  .NET Core 包含:

  • .NET Runtime(暫無)
  • .NET Core Framework

  編譯器、ASP.NET:

  • C# & Visual Basic ("Roslyn")
  • Visual F# Tools(暫無)
  • ASP.NET 5
  • Entity Framework

  1. What is .NET Core?

  答:.NET Core is a?modular development stack?that is the foundation of all future .NET platforms It’s already used by ASP.NET 5 and .NET Native.

  關(guān)鍵詞:Modular(模塊化),現(xiàn)在的 .NET Core 只是增加了 .NET Core Framework 的一些代碼,.NET Runtime 并未添加任何代碼,這個(gè)需要時(shí)間,既然代碼都未添加完善,那為什么 ASP.NET 5 and .NET Native 現(xiàn)在可以跨平臺(tái)了?因?yàn)樗麄儧]有使用 .NET Core,而是使用獨(dú)立的 Runtime-KRuntime,現(xiàn)在已更名為 XRE。

  2. Why do we open source .NET Core?

  比較“官方”的回答:

  • Lay the foundation for a cross platform .NET.
  • Build and leverage a stronger ecosystem.
  •   其實(shí)翻譯過來就是“形勢所逼”,但事實(shí)真是如此嗎?其實(shí)也不盡然,看這一段敘述就明白一些了:

    The challenge is that the Windows implementation has one code base while Mono has a completely separate code base. The Mono community was essentially forced to re-implement .NET because no open source implementation was available. Sure, the source code was available since Rotor but we didn’t use an OSI approved open source license, which made?Rotor?a non-starter. Customers have reported various mismatches, which are hard to fix because neither side can look at the code of the other side. This also results in a lot of duplicated work in areas that aren’t actually platform specific. A recent example is?immutable collections.

      這段敘述是很有意思的,因?yàn)?.NET 的不開源,Mono 社區(qū)重寫了跨平臺(tái)的 .NET,又因?yàn)?Rotor 沒有基于任何 OSI 開源協(xié)議實(shí)現(xiàn),所以導(dǎo)致 Mono 的實(shí)現(xiàn)充滿了困難,這些都是微軟親自承認(rèn)的,想想在以前,微軟會(huì)說這些嗎?相反它會(huì)高傲的抬起它的頭,其實(shí)從這一方面就可以看出,Mono 社區(qū)這么多年是多么的不容易,微軟這次的改變也是多么的不容易。為了加深你的印象,微軟列舉了一個(gè)最新例子:immutable collections,Mono 團(tuán)隊(duì)是這樣寫的說明:Unfortunately Microsoft didn't release their implementation under a license useable in mono I needed to reimplement the interfaces.,其實(shí)你可以看出一種無奈,Immutable Collections 是 .NET Framework 的基礎(chǔ)核心類庫之一,這還是之一,當(dāng)然還有很多的實(shí)現(xiàn),試想一下,如果 .NET 早就開放 .NET Framework 的源代碼,即使微軟沒有對它進(jìn)行跨平臺(tái),也會(huì)減少 Mono 團(tuán)隊(duì)的開發(fā)難度,以及盡量減少與 Windows 平臺(tái)實(shí)現(xiàn)應(yīng)用程序的兼容問題,當(dāng)然這都是后話。

      Build and leverage a stronger ecosystem.

      關(guān)于這部分內(nèi)容的解讀,微軟主要說了兩點(diǎn):Nuget 和 Interact,關(guān)于 Nuget,dudu 之前好像吐槽了好多次,但是微軟說了,我們花了兩年的時(shí)間去做這個(gè)東西,并得到了很好的反饋,但好像得到的反饋沒有來自國內(nèi),使用 Nuget 的用意,其實(shí)就是之前說到的 Modular(模塊化、組件化),這樣的好處就是很好的擴(kuò)展和維護(hù),Interact 可以理解為開源帶來的交互和進(jìn)步,可以讓開發(fā)者參與,并且反饋問題,利用社區(qū)的力量,讓 .NET 更加完善。很有意思的是,在最后作者舉了一個(gè)生活的例子:我認(rèn)為這就像駕駛一輛汽車,方向盤小的調(diào)整比大的調(diào)整,更加有效果,而且也更加安全,這個(gè)小的調(diào)整可以看作是來自社區(qū)的那一份份“微小”的力量,大的調(diào)整,當(dāng)然是微軟自己單獨(dú)搞,想想就知道哪個(gè)比較靠譜點(diǎn)。

      3. Our choice of using GitHub

      這部分也是非常有意思的,首先,微軟做了一個(gè)調(diào)查,發(fā)現(xiàn)大部分的 .NET 社區(qū)都活躍在 GitHub 上,Don’t believe it?(看到這,我大笑了三聲),不相信的話,微軟給你舉了一個(gè)真實(shí)的例子,nuproj(作者的一個(gè)個(gè)人項(xiàng)目),在 CodePlex 上放了兩年,僅僅得到了一個(gè) Pull 請求(想想也挺傷心的),然后把它移到 GitHub 上,僅僅五天后,就收到了三個(gè) Pull 請求和兩個(gè)提交,三個(gè)月的時(shí)間,總共收到了 16 個(gè) Pull 請求和一些很有價(jià)值的提交。

      作者的感受:one of the first ones was around adding unit testing, how awesome is that? 看來他是非常的激動(dòng)!

      需要注意的是,CodePlex 是微軟構(gòu)建的 Open Source 網(wǎng)站,它的競爭對手就是 GitHub,而微軟卻如此諷刺自己的“孩子”,并且放棄了它,選擇了別人家的孩子,并把自己的“財(cái)產(chǎn)”傳給它,這是需要多么大的勇氣啊,雖然是迫不得已,但讓一個(gè)巨頭這樣低頭也是一件很難得的事(此處應(yīng)該有掌聲)。

      4. Development in the open

      這部分主要是講微軟如何做開源開發(fā),提到了他們之前做的一個(gè)開源項(xiàng)目-Managed Extensibility Framework (MEF),他們認(rèn)為這是失敗的,原因很簡單,就是缺少社區(qū)的參與,剛才打開這個(gè)項(xiàng)目看了一下,基本上冷冷清清的,沒有一個(gè) Pull 請求(可以認(rèn)為沒人理它),文中提到了一個(gè)詞?code bombs,不知道翻譯為“代碼炸彈”合不合適?什么是 code bombs 呢?結(jié)合文中的敘述,談一下自己的理解,當(dāng)開發(fā)一個(gè)開源項(xiàng)目,其實(shí)最重要的就是社區(qū)的參與,以及把每個(gè)代碼提交都盡可能的細(xì)化、規(guī)范和公開,因?yàn)槟悴皇亲约涸陂_發(fā)這個(gè)項(xiàng)目,是整個(gè)開源社區(qū)在做,你需要考慮的更加全面,但往往現(xiàn)實(shí)是這樣的,維護(hù)團(tuán)隊(duì)因?yàn)榈貌坏缴鐓^(qū)的反饋,開發(fā)或修復(fù)一個(gè)功能沒有及時(shí)提交,總是隔很長的時(shí)間進(jìn)行“整坨”的提交,這時(shí)候如果項(xiàng)目出現(xiàn)什么問題,提交的代碼將會(huì)非常的難以查看和修復(fù),而且提交的過程中不注意規(guī)范,最后變成“一坨X”,也就是“代碼炸彈”,為了避免這種情況的發(fā)生,所以微軟做了以下總結(jié):

    • Code reviews. We also want to have all code reviews the team is doing to be in the public as well, via?GitHub’s pull request model.

    • Design documents & discussions. We’ll also share design notes, specs, and implementation specific documentation. We’ll need to figure out exactly what format we’ll be using. At a minimum you can expect Markdown based documents, similar to?Mad’s C# design notes. Another idea we had was recording our design meetings and share them on Channel 9. We need to figure out how we can do this on a somewhat regular cadence.

      其實(shí)總結(jié)就是一個(gè)詞:規(guī)范化,這樣才會(huì)讓項(xiàng)目更加“健壯”,之后提到了如果出現(xiàn)一些代碼問題,該如何反饋,以及微軟如何進(jìn)行解決,這個(gè)就略過了,如果你發(fā)現(xiàn)了一些問題,可以使用 GitHub issue 進(jìn)行反饋,微軟說了,我們會(huì)立即處理,呵呵。

      后面又講述了兩點(diǎn):We accept contributions、Building and running your own forks,主要說明你如何進(jìn)行代碼提交,奈何自己功力還沒達(dá)到這個(gè)境界,就此略過。

      5. .NET Foundation

      .NET Foundation(.NET 基金會(huì)):http://www.dotnetfoundation.org/

      .NET Foundation Projects:http://www.dotnetfoundation.org/projects

      說明:The .NET Core project is under the stewardship of the .NET Foundation. We believe that to be a critical part in promoting and advancing the .NET Core stack. We’re closely working with?Miguel de Icaza?from Xamarin/Mono in order to create a shared code base that can become a cross-platform implementation of .NET Core.

      上面提到了一個(gè)人名:Miguel de Icaza(米格爾·德伊卡薩),他是誰呢?他就是 Mono 的發(fā)起人,也是 Mono 團(tuán)隊(duì)的負(fù)責(zé)人,微軟成立 .NET Foundation 第一步的目的,其實(shí)說白了,就是要把 Mono 拉過來,共同把 .NET Core 給維護(hù)起來。很多人可能會(huì)有這樣的疑問:那 Mono 咋辦?在之前曾提到這樣一段話:The best way to build a cross-platform stack is to build a single stack, in a collaborative manner. And the best way to do exactly that is by open sourcing it.,關(guān)鍵詞:single、collaborative manner,這兩個(gè)詞就很好的說明了,現(xiàn)在 Mono 與 .NET Core 之間的關(guān)系,也就是說他們相互合作,共同維護(hù)和管理單一的 .NET 跨平臺(tái)實(shí)現(xiàn)(.NET Core),這也是社區(qū)的共同心愿。

      6. The relationship between Mono and .NET Core

      關(guān)于這個(gè)問題,上面可以看到一些答案,我們再來看一下更多的內(nèi)容。

      首先,在這篇博文中,有人這樣評(píng)論:I hope you join forces with the Mono team. Miguel de Icaza has been promoting this for years!

      作者回復(fù)的是一個(gè)?twitter 鏈接,內(nèi)容:

      如果有時(shí)間,可以再閱讀下這幾篇文章:

    • .NET Source Code Integration(來自 Mono 官網(wǎng))
    • Microsoft Open Sources .NET and Mono(作者為 Miguel de Icaza)
    • Introducing .NET Core(跳過前面內(nèi)容,直接查看 Mono 部分)

      摘錄自?.NET Source Code Integration?的部分內(nèi)容:

    We want to contribute some of the cross platform code from Mono, as well as chunks that we port from the .NET Framework Reference Source to the .NET Core effort.

    We will update this page with information as the porting process at Microsoft evolves and we can volunteer some of our/their code back to the .NET Core effort.

      摘錄自?Microsoft Open Sources .NET and Mono?的部分內(nèi)容:

    The code is available today from?http://github.com/Microsoft/referencesource. Mono will be able to use as much a it wants from this project.

    We have a project underway that already does this. We are replacing chunks of Mono code that was either incomplete, buggy, or not as fully featured as it should be with Microsoft's code.

    With the Mono project, we have spent 14 years working on open source .NET. Having Microsoft release .NET and issue a patent covenant will ensure that we can all cooperate and build a more vibrant, richer, and larger .NET community.

      仔細(xì)看 Miguel de Icaza 寫的博文內(nèi)容,你會(huì)感受到他當(dāng)時(shí)的心情,是多么的激動(dòng)(I am currently in NY celebrating :-)),后面大部分內(nèi)容介紹了 Mono 需要做的工作,下面我們來偵查一下他們是如何進(jìn)行交互工作的。

      文中提到,微軟開源了?Reference Source,referencesource 是什么?它其實(shí)就是 .NET Framework,只不過 GitHub 上并沒有全部開源,詳情請查看:http://referencesource.microsoft.com/,需要注意的是,這個(gè) .NET Framework 是 Windows 平臺(tái)下的,并不是我們所說的 .NET Core 的一部分,Mono 也 Fork 了這個(gè)項(xiàng)目,地址為:https://github.com/Microsoft/referencesource。

      我們再具體隨便看一下 referencesource 項(xiàng)目中一個(gè)具體類?System.ComponentModel.DataAnnotations.AssociationAttribute:

    #if !SILVERLIGHT using System.Diagnostics.CodeAnalysis;namespace System.ComponentModel.DataAnnotations {[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)][SuppressMessage("Microsoft.Performance", "CA1813:AvoidUnsealedAttributes", Justification = "We want users to be able to extend this class")]public class ScaffoldColumnAttribute : Attribute {public bool Scaffold { get; private set; }public ScaffoldColumnAttribute(bool scaffold) {Scaffold = scaffold;}}[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)][SuppressMessage("Microsoft.Performance", "CA1813:AvoidUnsealedAttributes", Justification = "We want users to be able to extend this class")]public class ScaffoldTableAttribute : Attribute {public bool Scaffold { get; private set; }public ScaffoldTableAttribute(bool scaffold) {Scaffold = scaffold;}} } #endif

      對應(yīng)于 .NET Core 中的實(shí)現(xiàn)?System.ComponentModel.DataAnnotations.AssociationAttribute:

    // Copyright (c) Microsoft. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information.namespace System.ComponentModel.DataAnnotations {[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]public class ScaffoldColumnAttribute : Attribute{public ScaffoldColumnAttribute(bool scaffold){Scaffold = scaffold;}public bool Scaffold { get; private set; }} }

      你會(huì)發(fā)現(xiàn),.NET Core 中的實(shí)現(xiàn)完全變了,還需要注意的是,在每個(gè)類的上面都有了開源協(xié)議說明(MIT license),這個(gè)和之前 referencesource 項(xiàng)目是完全不同的,關(guān)于代碼更改,這只是項(xiàng)目中的冰山一角,具體你可以對比查看兩個(gè)項(xiàng)目代碼到底有什么不同,有的被移除了,有的被重寫了,這樣改變的目的就是跨平臺(tái)。我們再看下 Mono 中這部分的代碼(Windows 平臺(tái)下),tree 目錄:mono/mono/tree/master/mcs/class/System.ComponentModel.DataAnnotations,查看里面的目錄結(jié)構(gòu),沒有發(fā)現(xiàn)一個(gè)源代碼類文件,只有一些文檔說明和單元測試代碼,難道 Mono 沒有實(shí)現(xiàn)嗎?答案當(dāng)然不是的,查看一下最近的一個(gè)代碼提交記錄?https://github.com/mono/mono/commit/c7405e6ca0b5749433c9c7acd49a5a8521ed9094,然后在?System.ComponentModel.DataAnnotations.dll.sources?文件中,發(fā)現(xiàn) Mono 進(jìn)行了如下更改:

    ../../build/common/Locale.cs../../build/common/MonoTODOAttribute.csAssembly/AssemblyInfo.cs -System.ComponentModel.DataAnnotations/AssociatedMetadataTypePropertyDescriptor.cs -System.ComponentModel.DataAnnotations/AssociatedMetadataTypeTypeDescriptor.cs -System.ComponentModel.DataAnnotations/AssociatedMetadataTypeTypeDescriptionProvider.cs -... +../../../external/referencesource/System.ComponentModel.DataAnnotations/DataAnnotations/AssociatedMetadataTypeTypeDescriptionProvider.cs +../../../external/referencesource/System.ComponentModel.DataAnnotations/DataAnnotations/AssociatedMetadataTypeTypeDescriptor.cs +../../../external/referencesource/System.ComponentModel.DataAnnotations/DataAnnotations/AssociationAttribute.cs +...

      返回上級(jí)、上級(jí)、上級(jí),進(jìn)入 external/referencesource 目錄,你看到了熟悉的東西,查找到這,你也應(yīng)該懂了,因?yàn)?referencesource 的開源(可以認(rèn)為是 Windows 平臺(tái)下的 .NET Framework),所以 Mono 沒必要用以前自己實(shí)現(xiàn)的代碼了,直接使用微軟開源出來的代碼,這是給 Mono 帶來的一個(gè)改變,關(guān)于上面 .NET Core 這部分的實(shí)現(xiàn)代碼,我想微軟也應(yīng)該參考了 Mono 跨平臺(tái)的實(shí)現(xiàn)代碼(非 Windows 平臺(tái)),從這一方面講,微軟和 Mono 社區(qū)是互惠的,就像 Miguel de Icaza 所講的那樣,we can all cooperate and build a more vibrant, richer, and larger .NET community. 其實(shí)大家都是為了 .NET 社區(qū)的更好發(fā)展,說到這,在他的這篇文章中,有一個(gè)有意思的評(píng)論(Miguel de Icaza 的回復(fù)也是很有意思):

      7. The relationship between .NET Core(corefx, CoreCLR) and XRE

      我們先來看一下,微軟在寫那篇文章的時(shí)候所列出的一些計(jì)劃:

    • More libraries. Consider the subset we have today a down-payment on what is to come. Our goal is to open source the entire .NET Core library stack by Build 2015.
    • Building and running on non-Windows platforms. We currently only provide the ability to build and run on Windows. We intend to build a public working group between us and the Mono community once we have enough code out there.
    • .NET Core Runtime (CoreCLR). We’re currently figuring out the plan for open sourcing the runtime. Stay tuned!

      再來看一下現(xiàn)在完成的進(jìn)度:

    • corefx:This repository contains the foundational libraries that make up the .NET Core development stack.
    • Roslyn:The .NET Compiler Platform ("Roslyn") provides open-source C# and Visual Basic compilers with rich code analysis APIs.
    • XRE:The .NET Cross-Platform Runtime SDK (.NET XRE) contains the code required to bootstrap and run an application, including the compilation system, SDK tools, and the native CLR hosts.
    • ....

      對比計(jì)劃和開源的項(xiàng)目,你可以往上面套一套了,首先,.NET Core 包含 .NET Core Framework(corefx,部分完成)和 .NET Core Runtime(CoreCLR,暫無),Roslyn 為跨平臺(tái)的編譯器,XRE 是什么?它是不是微軟未完成的 CoreCLR,其實(shí)并不是的,我們再來看開發(fā)者提給微軟的一些問題。

      1. 鏈接地址:.NET Core Runtime

      :I've wanted to learn about the .NET runtime for a while and have tried studying Rotor and the spec but haven't gotten very far. I was thinking of retrying with the .NET Core runtime but I can't seem to find it in the GitHub repository.

      :Excellent question. It is really two questions.

    • Where/when is CoreCLR? It is in a private repo on GitHub, but it isn't ready yet. We'll switch to public in the first part of next year. The team is focussed on making this happen.

      We've been thinking about how to make it easy for folks to learn about the runtime, in order to understand the code and make quality PRs. We have a bunch of internal documentation that we were thinking about making available in a WH wiki. Sound like a good idea?

    • We have a bunch of requirements that are really important to satisfy. On another thread in the forums here, we've been talking about the relationship between .NET Core and the .NET Framework. We need an efficient way to flow code back and forth between .NET Core and the .NET Framework. Getting that right takes time, but will also pay dividends and allow us to take PRs more efficiently once we go public.

      We had some of that infrastructure in place when we released the corefx repo, but we learned that it wasn't quite enough. We're taking more time upfront with the coreclr repo.

      I'm glad to hear that you and others are excited to see the coreclr repo show up. We are too.

      解讀:這個(gè)提問者很犀利,直接問為什么在 GitHub 上,找不到 .NET Core Runtime 的項(xiàng)目,原來它被藏起來了(Private),還沒有被開放出來,微軟正在加緊時(shí)間開發(fā)這個(gè)東西(最重視的項(xiàng)目),后面說明了 .NET Core and the .NET Framework 之間的關(guān)系,目前 corefx 是 .NET Core 的部分實(shí)現(xiàn),等到 coreclr 正式發(fā)布出來,才可以說它是完整的。

      2. 鏈接地址:.NET Core CLR Questions!

      :Can I embed .NET Core and invoke the runtime like I can with "Embedded Mono"?

      :The CLR has had a public hosting API since its inception. SQL Server and IIS are great examples of CLR hosts. CoreCLR also has a hosting API, which ASP.NET 5 uses. This will be included in the CoreCLR repo, when it shows up.

      :Will .NET Native be open sourced as well so we could port it to other platforms and CPU types? (This would be important to compile .NET on iOS).

      :Right now, we are focussed on CoreCLR. I can imagine .NET Native following. Right now, CoreCLR has all of our attention, so we'll re-visit the .NET Native question after we've got CoreCLR on GitHub.

      解讀:第一個(gè)問題是,現(xiàn)在能不能像 Mono 一樣啟動(dòng) .NET Core,答案是如果使用 ASP.NET 5 是可以的,因?yàn)?ASP.NET 5 已經(jīng)實(shí)現(xiàn)了,那就是 XRE,但 XRE 并不是 CoreCLR,它只是 CoreCLR 的一部分(included),第二個(gè)問題的答案在后半部分,我沒有貼出來,但這部分答案會(huì)讓你對 CoreCLR 有所期待,微軟正在集中力量去開發(fā)它,我們只能期待它早點(diǎn)正式發(fā)布。

      8. 有關(guān) .NET 開源的一些其他疑問(來自文章評(píng)論)

      1. 問:Does this mean yes or no to open-sourced WPF?

      :We currently don't plan on open sourcing the client stacks, which includes Windows Forms as well as WPF.

      暫時(shí)沒有計(jì)劃開放 Windows Forms、WPF 源代碼,是不是意味著被拋棄?

      2. 問:Will I ever be able to run Visual Studio on Linux?

      :I'm not working for Visual Studio but I haven't heard of any plans to offer a cross-platform version of Visual Studio itself.

      其實(shí)我還想問下 on Mac?呵呵,多希望回答的是 Absolutely,但是還是很期待微軟可以實(shí)現(xiàn)跨平臺(tái)的 Visual Studio,這樣才真正是宇宙最強(qiáng)大的 IDE!

      3. 問:Is there a roadmap to "upgrade" WPF to .Net Core 5?

      :There are currently no plans to port either WinForms or WPF to .NET Core.

      和第一個(gè)問題一樣,微軟沒打算把 WinForms 和 WPF 的代碼添加到 .NET Core 中。

      4. 問:does anyone know is plain old WCF a part of CORE?

      :The client side of WCF will be included in .NET Core.

      和 WinForms、WPF 相反,WCF 的代碼將會(huì)被添加到 .NET Core 中。

      5. 問:Immo, ok so all of this is leaving me a bit confused.What does all of this mean for me as an LOB developer?

      :The key difference is that .NET Core will be a single, cross platform stack.

      Let's contrast this with .NET Framework and Mono today. Mono is a full reimplementation of the .NET Framework. This means that certain features aren't supported or implemented differently enough to cause behavioral changes that can break your application. Also, Microsoft doesn't support running on Linux or MacOS today.

      .NET Core on the other hand will be supported by Microsoft on Windows, Linux and MacOS. We'll have a single code base that we plan on working on together with the Mono community.

      For you that means that targeting other platforms will be more reliable and innovation will happen faster. It also means that you'll be able to find out sooner what's coming next because all the design work is happening in public.

      Finally, you'll also be able to take a look at the kitchen and engage in design discussion.

      這個(gè)回答有點(diǎn)啰嗦,主要強(qiáng)調(diào)了 Mono 的貢獻(xiàn),然后再拉著 Mono 一塊做單一的跨平臺(tái) .NET Core,最后就是過程開放。

      插一段:including the entire CoreCLR runtime (JIT, GC, etc) as well as a set of class libraries. 可以看出 .NET Core 大致包含哪些東西。

      6. 問:Will everything eventually have an MIT license? Or the Apache 2.0 license? Does it now? I'm confused.

      :Your confusion is very understandable. Some of the .NET open source projects (such as Roslyn) use Apache 2.0. Newer projects (such as .NET Core) use the MIT license. Both licenses are OSI approved open source licenses so either one is indicative of OSS.

      Moving forward, we plan on exclusively using the MIT license. I don't know whether we'll switch the existing projects from Apache 2.0 to MIT.

      這個(gè)是關(guān)于開源協(xié)議的,我們可以通過查看開源項(xiàng)目中,比如類文件的最上面有協(xié)議說明,ASP.NET 中大部分項(xiàng)目都是基于 Apache 2.0,最新的開源項(xiàng)目(比如 corefx)是基于 MIT,不知道這兩個(gè)協(xié)議有什么不同,看作者的意思,微軟是想把 Apache 2.0 協(xié)議轉(zhuǎn)到 MIT 下,不知道可不可行。

      7. 問:WebForms on Linux and Mac OS?

      :We're making the .NET Core Framework cross plat, not the .NET Framework and .NET Core doesn't include WebForms.

      很簡單,No。

      8. 問:does this mean, i will be able to run .net desktop apps on linux, without the original developer having to port to mono?

      :We currently don't plan to open source the client technologies (WinForms, WPF), nor taking them cross plat. So I'm afraid the answer would be no.

      很簡單,No plan。

      9. 問:What is the relation between .NET Core and .NET Native?

      :.NET Native is two things: it's a tool chain that pre-compiles IL to native and it's a runtime that executes that code. .NET Core is CLI compliant stack that runs both, JIT based applications (via CoreCLR) as well as ahead-of-time compiled (AOT) applications (produced by .NET Native).

      這真是一個(gè)好問題,什么是 .NET Native?它最大的特點(diǎn),是可以直接將應(yīng)用程序代碼編譯成本地機(jī)器碼,這是和 .NET Core 最大的區(qū)別,所以作者提到了 CLI compliant,這部分在上面 Mono 中有說明。

      10. 問:You said that the client side of WCF will be included in .NET Core. What about the server side of WCF? I mean .NET Core will be a Server side technology if I understand correctly

      :That's an execellent question for the WCF team. I'll forward that question.

      這個(gè)上面有個(gè)問題搞錯(cuò)了,“client side of WCF will be included in .NET Core”,注意是 Client,而并不是 Server,這個(gè)兩個(gè)不同的概念,Client 的作用就是為了兼容之前開發(fā)的 WCF 項(xiàng)目。

      11. 問:.NET Core Runtime is the same runtime which .NET Framework uses?

      :.NET Core has two runtimes: CoreCLR and .NET Native.

      CoreCLR isn't identical to the CLR but it's very close. The key pieces are virtually identical, so same GC, same JIT, same type system etc.

      In .NET Native, it's obviously a lot more different as .NET Native doesn't have a JIT, but the GC, for example, is the same.

      How close is .NET Core Runtime to Mono Runtime?

      They are different implementations, done by different people. But both implement the same ECMA CLI standard, so they are quite similar in what they do, but most likely not in how they do it.

      好問題,好回答,CoreCLR 和 .NET Native 是完全不同的模式,.NET Native 因?yàn)橹苯涌梢灾v程序代碼編譯成本地機(jī)器碼,所以根本不需要 JIT,另外一個(gè)問題是 .NET Core Runtime 和 Mono Runtime,首先他們是不同的實(shí)現(xiàn),但他們都是基于 ECMA CLI(公共語言基礎(chǔ)),他們的相似之處都是將 CIL 編譯成本地機(jī)器碼。


      前段時(shí)間,dudu 寫了一篇這樣的博文:擋不住的好奇心:ASP.NET 5是如何通過XRE實(shí)現(xiàn)跨平臺(tái)的,最后產(chǎn)生了這樣一個(gè)疑問:在 Core CLR 被加載,Microsoft.AspNet.Hosting 被執(zhí)行之后,為什么還要用 Mono Runtime 加載一些 dotnet.host 相關(guān)的程序集?為什么不直接用 Core CLR 加載呢?

      其實(shí) XRE 并不是真正的 .NET Core Runtime,可以說是它的一部分,或者是一個(gè)先行版本,為什么要借助 Mono Runtime?很簡單,因?yàn)?.NET Core Runtime 并不完善,我相信微軟和 Mono 社區(qū)現(xiàn)在正在加緊完善這個(gè)最重要的項(xiàng)目 coreclr,至于 .NET Core Framework(corefx)的開發(fā),有了微軟的 Windows .NET Framework 和 Mono 自行實(shí)現(xiàn)的跨平臺(tái) .NET Framework,其實(shí)移植到 corefx 的難度并不大,重要的還是 coreclr,請注意這個(gè)鏈接地址:https://github.com/dotnet/coreclr,說不準(zhǔn)過幾天就不是“404”了,期待吧!

      如果你能堅(jiān)持看到這里,我真是感激涕零!!!

    轉(zhuǎn)載于:https://www.cnblogs.com/zjoch/p/6197025.html

    總結(jié)

    以上是生活随笔為你收集整理的魅力 .NET:从 Mono、.NET Core 说起的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。