.NET 5 的重大改變:消失的歷史技術

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在本文中,我們將回顧一些未能進入.NET Core的歷史性.NET技術。有趣之處在於,這些技術的API被複制過來了,這暗示着微軟當時在考慮將來在.NET Core中對它們進行實現。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"全局程序集緩存"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"全局程序集緩存(GAC)背後的理論是,所有.NET庫都可以存儲在單個集中的位置。在這種方式下,它與"},{"type":"link","attrs":{"href":"https:\/\/en.wikipedia.org\/wiki\/Component_Object_Model","title":"","type":null},"content":[{"type":"text","text":"COM"}]},{"type":"text","text":"庫類似。但與COM不同的是,它可以存儲每個庫的多個版本。通過這種方式,微軟希望可以避免困擾90年代應用程序的“DLL地獄”情景。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"但是,版本問題仍然存在。此外,獲得代碼簽名證書的需要以及Windows Vista帶來的安全性的增加使得GAC成爲一項令人討厭的技術。到.NET 4.5發佈時,幾乎沒有應用程序將GAC用於非微軟庫。主要的例外是商業庫,但即使是這些庫也已經轉向了對NuGet更友好的交付模型。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"因此,也就不奇怪,微軟在.NET Core中從根本上改變了他們的哲學。在新模型中,所有庫依賴項都與應用程序一起部署,從而使得應用程序可以與其他.NET Core應用程序隔離開來。因此,.NET Core中沒有GAC的概念。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"儘管如此,GAC API在.NET Core中仍然存在。它們所做的事情不多,例如,指示程序集是否在GAC中的屬性被硬編碼爲返回false。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"爲了進一步明確意圖,所有的GAC API現在都被標記爲已過時,微軟正考慮在未來的版本中刪除它們。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"Remoting"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https:\/\/en.wikipedia.org\/wiki\/.NET_Remoting","title":"","type":null},"content":[{"type":"text","text":".NET Remoting"}]},{"type":"text","text":"是受"},{"type":"link","attrs":{"href":"https:\/\/en.wikipedia.org\/wiki\/Distributed_Component_Object_Model","title":"","type":null},"content":[{"type":"text","text":"DCOM"}]},{"type":"text","text":"和"},{"type":"link","attrs":{"href":"https:\/\/en.wikipedia.org\/wiki\/Java_remote_method_invocation","title":"","type":null},"content":[{"type":"text","text":"Java Remoting(Java RMI)"}]},{"type":"text","text":"的啓發。這三種方法的基本思想都是一個應用程序可以使用代理對象來操作在另一個應用程序中運行的真實對象。雖然它在技術上可以工作,但.NET Remoting從來就沒有流行過,因爲要正確地使用它很難,而且人們一般認爲它很脆弱。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"考慮到這一點,.NET Core從未實現過.NET Remoting API。就像GAC API一樣,它只有不可操作的佔位符。因此,它們也被標記爲已過時,而最終目的是將其刪除。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"代碼訪問安全"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"繼續這個主題,"},{"type":"link","attrs":{"href":"https:\/\/docs.microsoft.com\/en-us\/dotnet\/framework\/misc\/code-access-security","title":"","type":null},"content":[{"type":"text","text":"代碼訪問安全(CAS)"}]},{"type":"text","text":"是另一種API被複制到.NET Core中,但被標記爲已過時的.NET Framework技術。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"代碼訪問安全創建於Docker等隔離容器之前。在.NET Framework時代,多個應用程序會託管在單個Internet Information Server(IIS)實例中。理論上,每個應用程序都將被隔離到一個單獨的應用程序域中,但要打破這種隔離並干擾在IIS中運行的其他應用程序並不難。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"代碼訪問安全的創建就是爲了限制這種可能的損害。其基本思想是,危險的API會被加上表示風險的屬性。IIS之類的主機可以配置爲運行具有不同“信任”級別的應用程序,從理論上講,是將它們放入一個沙箱中。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"CAS的另一個用途是用於瀏覽器託管的應用程序。早在Silverlight出現之前,就已經可以在Internet Explorer中運行Windows窗體應用程序了。應用程序的信任級別部分取決於它是從哪裏加載的,內部站點會獲得更高的權限。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"但是和許多早期的.NET技術一樣,要正確地實現CAS很困難。惡意應用程序有許多方法可以繞過CAS限制,而良性應用程序卻常常爲這些限制所限。結果,瀏覽器託管的應用程序很快就把它禁用了,而IIS在很大程度上忽略了CAS信任級別。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"Thread.Abort"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這可能會令你感到驚訝。"},{"type":"link","attrs":{"href":"https:\/\/docs.microsoft.com\/en-us\/dotnet\/api\/system.threading.thread.abort","title":"","type":null},"content":[{"type":"text","text":"Thread.Abort"}]},{"type":"text","text":"在.NET Core中從未實現過。雖然它總是被認爲有危險,但總也不可避免。在ASP.NET中,像請求超時或客戶端斷開連接這樣簡單的事情就會觸發一個"},{"type":"codeinline","content":[{"type":"text","text":"Thread.Abort"}]},{"type":"text","text":"調用。如果你沒有認真地編寫代碼進行處理,這可能會導致資源泄漏,比如獲取的鎖或打開的數據庫事務。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"到ASP.NET Core被創建時,"},{"type":"codeinline","content":[{"type":"text","text":"CancellationToken"}]},{"type":"text","text":"已成爲一個安全且被廣泛接受的"},{"type":"codeinline","content":[{"type":"text","text":"Thread.Abort"}]},{"type":"text","text":"替代者,因此就不需要在.NET Core的第一個版本中實現它了。儘管.NET Core已經將其功能擴展到Web站點之外,但其他主要的應用程序框架都不需要"},{"type":"codeinline","content":[{"type":"text","text":"Thread.Abort"}]},{"type":"text","text":",因此它會繼續拋出"},{"type":"codeinline","content":[{"type":"text","text":"PlatformNotSupportedException"}]},{"type":"text","text":"。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在.NET 5中,該方法終被標記爲已過時。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"原文鏈接:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https:\/\/www.infoq.com\/news\/2020\/12\/net-5-breaking-changes-2\/","title":"","type":null},"content":[{"type":"text","text":".NET 5 Breaking Changes: Historic Technologies"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章