CoreRT 初體驗:將 .NET Core 應用程式封裝成單一可執行檔

我之前曾經在 如何將 SCD 部署的 .NET Core 應用程式封裝單一可執行檔 (Warp) 文章中分享過 Warp 這套工具,也在該文最後分享 CoreRT 應該才是最終的解決方案。今天這篇文章我就來分享如何透過 CoreRT 來編譯你的 .NET Core 應用程式!
建立 .NET Core 主控臺應用程式
-
先限定 .NET CLI 使用版本
dotnet new globaljson --sdk-version 2.2.101
-
建立專案
dotnet new console -n c1 cd c1
設定 dotnet-core 套件來源
由於 CoreRT 目前還在 Alpha 階段,所有尚在開發、測試中的 .NET Core 工具、套件,預設都不會上架到 NuGet Gallery ,而是放在 dotnet-core 這裡。
-
建立 nuget.config 設定檔
我們必須先建立一個
nuget.config
設定檔,方便我們對專案設定 NuGet 套件來源。dotnet new nuget
-
新增
dotnet-core
套件來源加入 dotnet-core 套件來源到
nuget.config
之中。nuget sources add -Name dotnet-core -Source https://dotnet.myget.org/F/dotnet-core/api/v3/index.json
建立完成後的 nuget.config
內容如下:
<?xml version="1.0" encoding="utf-8"?> <configuration> <packageSources> <!--To inherit the global NuGet package sources remove the <clear/> line below --> <clear /> <add key="nuget" value="https://api.nuget.org/v3/index.json" /> <add key="dotnet-core" value="https://dotnet.myget.org/F/dotnet-core/api/v3/index.json" /> </packageSources> </configuration>
新增 Microsoft.DotNet.ILCompiler 套件
以下命令會自動安裝 1.0.0-alpha-*
最新版本:
dotnet add package Microsoft.DotNet.ILCompiler -v 1.0.0-alpha-*
直接透過 .NET CLI 的 SCD 進行建置
這時我們就可以好好比對一下目前 .NET Core SCD 與 CoreRT SCD 的差異之處!
-
使用 CoreRT 發行
dotnet publish -r win-x64 -c Release -o dist-corert
總建置大小約為
15MB
左右,檔案數量為4
個,而且啟動速度超快! -
使用傳統 .NET Core SCD 發行
我們只要移除 Microsoft.DotNet.ILCompiler 套件,就可以切回目前的編譯方式。
dotnet remove package Microsoft.DotNet.ILCompiler dotnet publish -r win-x64 -c Release -o dist-netcore
總建置大小約為
66.1MB
左右,檔案數量為217
個。
數字可以證明一切, CoreRT 就是未來的明日之星,完全可以期待它的到來!
本篇文章的命令整理
dotnet new globaljson --sdk-version 2.2.101 dotnet new console -n c1 cd c1 dotnet new nuget nuget sources add -Name dotnet-core -Source https://dotnet.myget.org/F/dotnet-core/api/v3/index.json dotnet add package Microsoft.DotNet.ILCompiler -v 1.0.0-alpha-* dotnet publish -r win-x64 -c Release -o dist-corert dotnet remove package Microsoft.DotNet.ILCompiler dotnet publish -r win-x64 -c Release -o dist-netcore