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

栏目: ASP.NET · 发布时间: 7年前

内容简介:我之前曾經在由於
CoreRT 初體驗:將 .NET Core 應用程式封裝成單一可執行檔

我之前曾經在 如何將 SCD 部署的 .NET Core 應用程式封裝單一可執行檔 (Warp) 文章中分享過 Warp 這套工具,也在該文最後分享 CoreRT 應該才是最終的解決方案。今天這篇文章我就來分享如何透過 CoreRT 來編譯你的 .NET Core 應用程式!

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

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

  1. 先限定 .NET CLI 使用版本

    dotnet new globaljson --sdk-version 2.2.101
  2. 建立專案

    dotnet new console -n c1
    cd c1

設定 dotnet-core 套件來源

由於 CoreRT 目前還在 Alpha 階段,所有尚在開發、測試中的 .NET Core 工具、套件,預設都不會上架到 NuGet Gallery ,而是放在 dotnet-core 這裡。

  1. 建立 nuget.config 設定檔

    我們必須先建立一個 nuget.config 設定檔,方便我們對專案設定 NuGet 套件來源。

    dotnet new nuget
  2. 新增 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 的差異之處!

  1. 使用 CoreRT 發行

    dotnet publish -r win-x64 -c Release -o dist-corert

    總建置大小約為 15MB 左右,檔案數量為 4 個,而且啟動速度超快!

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

  2. 使用傳統 .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 初體驗:將 .NET Core 應用程式封裝成單一可執行檔

數字可以證明一切, 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

相關連結


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Programming Collective Intelligence

Programming Collective Intelligence

Toby Segaran / O'Reilly Media / 2007-8-26 / USD 39.99

Want to tap the power behind search rankings, product recommendations, social bookmarking, and online matchmaking? This fascinating book demonstrates how you can build Web 2.0 applications to mine the......一起来看看 《Programming Collective Intelligence》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具