.NET Core 在發佈應用程式主要有三種模式:
其中常見的以前兩者為主,今天也就先簡單介紹這兩種模式以及如何透過 dotnet publish
進行發布。
為了方便示例,先建立一個 console 專案,並且透過 dotnet run
去執行看預期結果:
$ dotnet new console -o CLIProj
The template "Console Application" was created successfully.
Processing post-creation actions...
Running 'dotnet restore' on CLIProj/CLIProj.csproj...
Restore completed in 176.45 ms for /Users/fntsr/Projects/Ironman/dotnet-sln/CLIProj/CLIProj.csproj.
Restore succeeded.
$ cd CLIProj/
$ dotnet run
Hello World!
接著我們嘗試使用 dotnet publish
並且不搭配任何參數,通常這樣的發佈模式預設會是 FDD:
$ dotnet publish
Microsoft (R) Build Engine version 16.2.32702+c4012a063 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.
Restore completed in 29.46 ms for /Users/fntsr/Projects/Ironman/dotnet-sln/CLIProj/CLIProj.csproj.
CLIProj -> /Users/fntsr/Projects/Ironman/dotnet-sln/CLIProj/bin/Debug/netcoreapp2.2/CLIProj.dll
CLIProj -> /Users/fntsr/Projects/Ironman/dotnet-sln/CLIProj/bin/Debug/netcoreapp2.2/publish/
此時我們可以透過 dotnet
指令去執行發佈出來的 dll 檔案:
$ dotnet ./bin/Debug/netcoreapp2.2/publish/CLIProj.dll
Hello World!
但若我們直接想嘗試直接執行話,就會出錯:
$ ./bin/release/netcoreapp2.2/publish/CLIProj.dll
fish: The file './bin/release/netcoreapp2.2/publish/CLIProj.dll' is not executable by this user
這就是 FDD 模式,如其名,是相依 Framework。
若是我們要發布一個可以執行的程式又該如何呢?這時就可使用 SCD 了!若我們在發布參數中透過 --runtime
指定了執行階段,就會預設開啟 SCD 模式:
$ dotnet publish -c release -r osx-x64
Microsoft (R) Build Engine version 16.2.32702+c4012a063 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.
Restore completed in 242.13 ms for /Users/fntsr/Projects/Ironman/dotnet-sln/CLIProj/CLIProj.csproj.
CLIProj -> /Users/fntsr/Projects/Ironman/dotnet-sln/CLIProj/bin/release/netcoreapp2.2/osx-x64/CLIProj.dll
CLIProj -> /Users/fntsr/Projects/Ironman/dotnet-sln/CLIProj/bin/release/netcoreapp2.2/osx-x64/publish/
這時就可以直接執行了!
$ ./bin/release/netcoreapp2.2/osx-x64/publish/CLIProj
Hello World!