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

我之前曾經在 [如何將 SCD 部署的 .NET Core 應用程式封裝單一可執行檔 (Warp)](/post/2019/01/14/Useful-tool-Warp-Create-self-contained-single-binary-applications.aspx) 文章中分享過 [Warp](https://github.com/dgiagio/warp) 這套工具，也在該文最後分享 [CoreRT](https://github.com/dotnet/corert/blob/master/Documentation/intro-to-corert.md) 應該才是最終的解決方案。今天這篇文章我就來分享如何透過 **CoreRT** 來編譯你的 .NET Core 應用程式！ 

### 建立 .NET Core 主控台應用程式

1.  先限定 .NET CLI 使用版本
    
    ```bash
    dotnet new globaljson --sdk-version 2.2.101
    ```
    
2.  建立專案
    
    ```bash
    dotnet new console -n c1
    cd c1
    ```
    

### 設定 dotnet-core 套件來源

由於 [CoreRT](https://github.com/dotnet/corert/) 目前還在 Alpha 階段，所有尚在開發、測試中的 .NET Core 工具、套件，預設都不會上架到 [NuGet Gallery](https://www.nuget.org/profiles/Microsoft)，而是放在 [dotnet-core](https://web.archive.org/web/20210118202539/https://dotnet.myget.org/gallery/dotnet-core) 這裡。

1.  建立 nuget.config 設定檔
    
    我們必須先建立一個 `nuget.config` 設定檔，方便我們對專案設定 NuGet 套件來源。
    
    ```bash
    dotnet new nuget
    ```
    
2.  新增 `dotnet-core` 套件來源
    
    加入 [dotnet-core](https://web.archive.org/web/20210118202539/https://dotnet.myget.org/gallery/dotnet-core) 套件來源到 `nuget.config` 之中。
    
    ```bash
    nuget sources add -Name dotnet-core -Source https://dotnet.myget.org/F/dotnet-core/api/v3/index.json
    ```
    

建立完成後的 `nuget.config` 內容如下：

```xml
<?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](https://www.nuget.org/packages/Microsoft.DotNet.ILCompiler) 套件

以下命令會自動安裝 `1.0.0-alpha-*` 最新版本：

```bash
dotnet add package Microsoft.DotNet.ILCompiler -v 1.0.0-alpha-*
```

### 直接透過 .NET CLI 的 SCD 進行建置

這時我們就可以好好比對一下目前 .NET Core SCD 與 CoreRT SCD 的差異之處！

1.  使用 CoreRT 發行
    
    ```bash
    dotnet publish -r win-x64 -c Release -o dist-corert
    ```
    
    總建置大小約為 `15MB` 左右，檔案數量為 `4` 個，而且啟動速度超快！
    
    ![](https://stwillblogassets.blob.core.windows.net/files/images/external/stwillblogassets.blob.core.windows.net/9e6baaa43e0ee433c0df-dist-core.png)
    
2.  使用傳統 .NET Core SCD 發行
    
    我們只要移除 [Microsoft.DotNet.ILCompiler](https://www.nuget.org/packages/Microsoft.DotNet.ILCompiler) 套件，就可以切回目前的編譯方式。
    
    ```bash
    dotnet remove package Microsoft.DotNet.ILCompiler
    dotnet publish -r win-x64 -c Release -o dist-netcore
    ```
    
    總建置大小約為 `66.1MB` 左右，檔案數量為 `217` 個。
    
    ![](https://stwillblogassets.blob.core.windows.net/files/images/external/stwillblogassets.blob.core.windows.net/9e76ec8e0c783b560994-dist-netcore.png)
    

數字可以證明一切，[CoreRT](https://github.com/dotnet/corert/blob/master/Documentation/intro-to-corert.md) 就是未來的明日之星，完全可以期待它的到來！

### 本篇文章的命令整理

```bash
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
```

### 相關連結

-   [Intro to CoreRT](https://github.com/dotnet/corert/blob/master/Documentation/intro-to-corert.md)
    -   [.NET Core Runtime (CoreRT)](https://github.com/dotnet/corert)
-   [CoreRT - A .NET Runtime for AOT · Performance is a Feature!](https://mattwarren.org/2018/06/07/CoreRT-.NET-Runtime-for-AOT/)
-   [dotnet-core - Microsoft.DotNet.ILCompiler 1.0.0-alpha-\* | MyGet](https://www.nuget.org/packages/Microsoft.DotNet.ILCompiler)
-   [Building a Hello World console app with CoreRT](https://github.com/dotnet/corert/tree/master/samples/HelloWorld)
-   [Single exe self contained console app · Issue #13329 · dotnet/corefx](https://github.com/dotnet/corefx/issues/13329)
