.NET開發筆記:如何將.NET項目遷移到.NET Core


很多.net項目在開發的時候,.net core還沒有出現或者還麼有成熟,如今.netcore3.1已經出現,其技術風險已經比較低,今天對項目如何遷移到.net core做一個簡單的梳理,瑾做參考。

爲什麼要從.NET遷移到.NET Core?

.NET Core提供的特性

1.免費和最寬鬆的開源協議

.NET Core從屬於.NET基金會,由微軟進行官方支持。使用最寬鬆的MIT和Apache 2開源協議,文檔協議遵循CC-BY。這將允許任何人任何組織和企業任意處置,包括使用,複製,修改,合併,發表,分發,再授權,或者銷售。唯一的限制是,軟件中必須包含上述版權和許可提示,後者協議將會除了爲用戶提供版權許可之外,還有專利許可,並且授權是免費,無排他性的(任何個人和企業都能獲得授權)並且永久不可撤銷,用戶使用.NET Core完全不用擔心收費問題,你可以很自由的部署在任何地方.

2.輕量級、跨平臺

3.組件化、模塊化、IOC+Nuget、中間件

4.高性能

5.統一了MVC和WebAPI編程模型

a) 比如:ASP.NET Core 中MVC 和Web API 直接或間接繼承同一個基類 ControllerBase,提供可使用的API也一致化

b) 比如:舊ASP.NET時代,寫全局filter需要針對MVC 和Web API 分別編寫代碼,但在ASP.NET Core,直接使用一套中間件代碼即可

6.可測試性

7.微服務、容器化支持

8.標準化 .NET API 規範- .NET STANDARD,整體架構如下圖:

http://img2.mukewang.com/5e4bd8bd0001a75606510377.jpg 

.NET Core性能提升

1.TechEmpower機構

  TechEmpower機構會定期對各語言主流的web框架做性能測試

  地址:https://www.techempower.com/benchmarks/

http://img4.mukewang.com/5e4bd8cc000150fc10120338.jpg 

2.博客園博主、微軟MVP – 楊曉東,做的 “Hello World”性能測試

ASP.NET Core 1.1性能對比評測(ASP.NET,Python,Java,NodeJS)

地址:https://www.cnblogs.com/savorboard/archive/2016/10/17/dotnet-benchmarks.html

http://img3.mukewang.com/5e4bd8f500019cda07510834.jpg

http://img.mukewang.com/5e4bd8f50001a41905410326.jpg

性能測試工具:

微軟出品:Benchmarks

地址:https://github.com/aspnet/benchmarks

.NET如何遷移到.NET Core?

遷移工作量評估(API兼容性分析)

度量.net framework 遷移到.net core的工作量

地址:https://www.cnblogs.com/vveiliang/p/7402409.html

可移植性分析工具:.NET Portability Analyzer

地址:https://github.com/Microsoft/dotnet-apiport

【Cli 版本】  【Visual Studio Extension 版本】

地址:https://github.com/Microsoft/dotnet-apiport/tree/dev/docs/VSExtension

評估會給出類似如下報告:

http://img4.mukewang.com/5e4bd9120001f43e11190057.jpg

http://img1.mukewang.com/5e4bd91200017c1012860181.jpg

.NET Standard Versions

地址:https://github.com/dotnet/standard/blob/master/docs/versions.md

http://img1.mukewang.com/5e4bd92000019a7906490260.jpg

遷移方案制定

微軟官方《組織項目以支持 .NET Framework 和 .NET Core》文章中,有介紹兩種遷移方案:

方案一、將現有項目和 .NET Core 項目合併爲單個項目(多目標框架)

方案二、將現有項目和新的 .NET Core 項目分離(拷貝到新項目)

通過類名、命名空間,查詢API的實現信息

遷移過程中,有類庫命名空間被調整,nuget歸屬包被調整,具體類庫在.NET Core哪個版本中被實現也不是很清楚,通過下面兩個查詢助手,即可解決這些問題。

1、查詢命名空間、類庫在哪個nuget package包中定義(第一次查詢比較慢)

地址:https://packagesearch.azurewebsites.net/

2、查詢 NET API 在各版本中實現

地址:https://apisof.net/catalog/

結果圖如下:

http://img.mukewang.com/5e4bd9480001df5306800334.jpg

http://img1.mukewang.com/5e4bd9480001471710450512.jpg



類庫項目、應用項目如何選擇框架類型

1.如何組織一個同時面向多目標框架的 C# 項目解決方案

地址:https://walterlv.com/post/organize-csharp-project-targeting-multiple-platforms.html

2..NetStandard;.netCore;FX(.NetFrameWork)之間引用關係

在 .NET Standard 2.0中,我們使在 .NET Standard 庫中通過一個兼容層去引用已有的 .NET Framework 的庫成爲可能。當然,這隻對那些只使用了適用於 .NET Standard API 的 .NET Framework 庫有效。

http://img1.mukewang.com/5e4bd9570001f68c06870193.jpg 

FX引用.NetCore:不通過

.NetStandard引用.NetCore:不通過

.NetCore引用FX:通過

.NetCore引用.NetStandard:通過

.NetStandard引用FX:通過

FX引用.NetStandard:通過 

 ASP.NET Core可以在“.NET Framework 和 .NET Core”上運行,但ASP.NET Core 3.0 以及更高版本只能在 .NET Core 中運行。

具體參見:

【譯】介紹 .NET Standard 

地址:https://www.cnblogs.com/jinanxiaolaohu/p/10681024.html

關於.net core 和 .net fx相互引用的問題

地址:https://q.cnblogs.com/q/103398/

.NetStandard;.netCore;FX(.NetFrameWork)之間引用關係

地址:https://www.cnblogs.com/xiaoliangge/p/7475268.html

3.項目支持多目標框架

支持多目標框架,並解決第三方庫引用差異的問題(在 csproj 文件中指定包含條件)

地址:https://walterlv.com/post/configure-projects-to-target-multiple-platforms.html

目標框架名稱列表 (命名全小寫)

地址:https://docs.microsoft.com/zh-cn/nuget/reference/target-frameworks

如何調試多目標框架類庫?

地址:https://docs.microsoft.com/zh-cn/nuget/reference/target-frameworks

NET Standard/Core項目使用條件判斷輸出多版本xml註釋文檔

地址:https://blog.csdn.net/starfd/article/details/78839704

http://img4.mukewang.com/5e4bd96000014ede03060218.jpg

4.多目標框架中共享代碼方案

1、【首選】.NET Standard,需要目標SDK支持對應的 .NET Standard 版本。

2、共享項目。直接共享了源碼,只要在目標項目中指定了條件編譯符,那麼源碼便能針對各種不同的目標框架進行分別編譯。

3、[add as link]使用鏈接共享 Visual Studio 中的代碼文件

地址:https://blog.csdn.net/starfd/article/details/78839704

5.條件編譯符號 (命名全大寫)

指令:#if #elif #else #endif (||、&&、!)

因爲目標框架提供的API不相同。故必要時可添加條件編譯符號以便支持不同的運行時版本。

微軟有針對各個目標框架有預定義預處理器符號,vs編譯時會自動識別到這些框架條件編譯符號。

.NET Framework 4.5 --> NET45

.NET Framework 4.6 --> NET46

.NET Framework 4.6.1 --> NET461

.NET Standard 1.0 --> NETSTANDARD1_0

.NET Standard 1.6 --> NETSTANDARD1_6

.NETCOREAPP 2.0 --> NETCOREAPP2_0

參考:

#if指令文檔

地址:https://blog.csdn.net/starfd/article/details/78839704

如何將條件編譯符號(DefineConstants)傳遞給msbuild

地址:http://www.voidcn.com/article/p-nsrcccet-btr.html

遷移到.NET Core,但只運行在windows平臺上

使用 Windows 兼容性包將代碼移植到 .NET Core

地址:https://docs.microsoft.com/zh-cn/dotnet/core/porting/windows-compat-pack

如何:將 Windows 窗體桌面應用程序移植到 .NET Core

地址:https://docs.microsoft.com/zh-cn/dotnet/core/porting/winforms

如何:將 WPF 桌面應用移植到 .NET Core

地址:https://docs.microsoft.com/zh-cn/dotnet/core/porting/wpf

兼容.NET Core或.NET Standard API

遷移HttpHandler與HttpModule到 ASP.NET Core 中間件

地址:https://docs.microsoft.com/en-us/aspnet/core/migration/http-modules?view=aspnetcore-2.2

.NET CORE 2.0 踩坑記錄之ConfigurationManager

地址:https://www.cnblogs.com/binbinxu/p/7440342.html

.NET Core/Standard 2.0 編譯時報“CS0579: Duplicate 'AssemblyFileVersionAttribute' attribute”錯誤

1.自動生成AssemblyInfo的原理

地址:https://www.cnblogs.com/binbinxu/p/7440342.html

2.解決方案:不自動創建 AssemblyInfo 文件

地址:https://cloud.tencent.com/developer/article/1402303

ASP.NET Core開發之HttpContext

地址:https://www.cnblogs.com/linezero/p/6801602.html

.net core 使用JsonConvert替代JavaScriptSerializer

地址:https://my.oschina.net/idoop/blog/915555

ASP.NET Core 中重複讀取 Request.Body 的正確姿勢

地址:http://www.cnblogs.com/dudu/p/9190747.html

JsonRequestBehavior在core中被移除的問題

1.[ASP.NET MVC]解決"若要允許 GET請求,請將 JsonRequestBehavior 設置爲AllowGet"

地址:https://shiyousan.com/post/635428880708292949

2.[ASP.NET Core]core中不會拋出AllowGet異常,對於DenyGet則使用[HttpPost]代替

地址:

https://stackoverflow.com/questions/38578463/asp-net-core-the-name-jsonrequestbehavior-does-not-exist-in-the-current-cont

 

https://stackoverflow.com/questions/8464677/why-is-jsonrequestbehavior-needed?r=SearchResults

獲取ASP.NET Core中的Web根路徑和內容根路徑(移除的MapPath)

地址:http://beidouxun.com/Articles/Details/419f4786-679c-4316-b2bb-baea26c94205

爲什麼我的會話狀態在ASP.NET Core中不工作了?

地址:https://www.cnblogs.com/lwqlun/p/10526380.html

由於ASP.NET Core 2.1中引入的Cookie同意和非必要cookie的GDPR功能引起。(GDPR,即General Data Protection Regulation,《通用數據保護條例》) 

在ASP.NET4.5和ASP.NET Core中共享cookies認證信息(加解密方式不一致問題)

.NET跨平臺之旅:ASP.NET Core從傳統ASP.NET的Cookie中讀取用戶登錄信息

地址:https://www.cnblogs.com/cmt/p/5940796.html

開源方案:[git]idunno.CookieSharing

地址:https://github.com/blowdart/idunno.CookieSharing

ASP.NET Core如何獲取客戶端IP地址(被移除的ServerVariables對象)

地址:https://www.cnblogs.com/dudu/p/5972649.html

如何獲取項目依賴的所有程序集

方案一:AppDomain.CurrentDomain.GetAssemblies(),此辦法不能獲取到所有依賴的程序集,因爲.Net有延遲加載程序集機制

方案二:適用於傳統ASP.NET項目,System.Web.Compilation.BuildManager.GetReferencedAssemblies();

方案三:適用於ASP.NET Core項目,DependencyContext.Default.CompileLibraries,具體代碼參考下面

http://img3.mukewang.com/5e4bd9720001605205850543.jpg

參考:

AppDomain.CurrentDomain.GetAssemblies()

地址:https://www.cnblogs.com/beixing/p/3803874.html

Is there an alternative for BuildManager.GetReferencedAssemblies() in ASP.NET Core? 

地址:https://stackoverflow.com/questions/53989393/is-there-an-alternative-for-buildmanager-getreferencedassemblies-in-asp-net-co

.NET Core 2.0遷移System.Runtime.Caching

地址:https://www.cnblogs.com/mantgh/p/7429551.html

.NET Core 2.0升級到3.0實戰方案

地址:https://www.cnblogs.com/laozhang-is-phi/p/11520048.html

從.NET CORE2.2升級到3.0過程及遇到的一些問題

地址:https://www.cnblogs.com/roadflow/p/11711686.html 

遷移EF框架

【微軟官方】從 EF6 到 EF Core的遷移系列

地址:https://www.cnblogs.com/roadflow/p/11711686.html

【微軟官方】EF Core 版本升級遷移系列

地址:https://docs.microsoft.com/zh-cn/ef/core/what-is-new/ef-core-2.2

修改爲 ASP.NET Core 風格的應用

  按照上面的方式修改API兼容性後,框架類庫可以順利遷移到.NET Core,編譯通過,代碼運行也正常。但是ASP.NET Core風格的應用,其寫法也是有很多變化的。具體參考下面文檔:

將傳統 ASP.NET 應用遷移到 .NET Core

地址:https://mp.weixin.qq.com/s?__biz=MzAwNTMxMzg1MA==&mid=2654072516&idx=3&sn=af3916ec4e2c31c231f5f01be75712d8&chksm=80dbca91b7ac43870a3fa4dce10f5a97adf1548d1381a99f27972c67c00a1e593aa926b110ad&mpshare=1&scene=23&srcid=0311kxbJ5rwkIYu7xO2Yx08U

【微軟官方】ASP.NET 遷移到 ASP.NET Core教程系列

地址:https://docs.microsoft.com/zh-cn/aspnet/core/migration/proper-to-2x/?view=aspnetcore-2.2

【微軟官方】ASP.NET Core 版本升級遷移系列

地址:https://docs.microsoft.com/zh-cn/aspnet/core/migration/20_21?view=aspnetcore-2.2

相關文章:

.NET Core 3中的性能提升(譯文)

地址:https://zhuanlan.zhihu.com/p/66152703

Java(11,12) 與 .NET Core(2.2,3.0)的性能對比測試

地址:https://blog.csdn.net/hez2010/article/details/86551299

載自:cnblog.windy


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章