.NET Core 2.0以及之前版本控制台项目,默认都是生成的.dll文件,本文主要分享一下生成可执行文件exe(包括mac,windows,linux上)的方法,不用使用命令运行,独立布署.NET Core项目。

1、配置.csproj项目文件

出于调试目的,您可以使用dll。你可以使用它来运行它dotnet ConsoleApp2.dll。如果要生成exe,则必须生成独立布署的应用程序。

要生成独立布署的应用程序(在Windows中为exe),您必须指定目标运行时。

首先,在.csproj项目文件(支持的rid列表)中添加目标运行时的运行时标识符:

<PropertyGroup>
<RuntimeIdentifiers>win10-x64;ubuntu.16.10-x64</RuntimeIdentifiers>
</PropertyGroup>

2、独立发布命令

从.NET Core 2.0开始不再需要修改上述项目文件。发布应用程序时所需的命令如下:

1)Windows平台

 dotnet publish -c Release -r win10-x64

其它可选平台,

可移植:win-x86、win-x64
Windows 7 / Windows Server 2008 R2:win7-x64、win7-x86
Windows 8 / Windows Server 2012:win8-x64、win8-x86、win8-arm
Windows 8.1 / Windows Server 2012 R2:win81-x64、win81-x86、win81-arm
Windows 10 / Windows Server 2016:win10-x64、win10-x86、win10-arm、win10-arm64

2)Linux平台

dotnet publish -c Release -r ubuntu.16.10-x64

其它可选平台,

可移植:linux-x64
CentOS:centos-x64、centos.7-x64
Debian:debian-x64、debian.8-x64、debian.9-x64(.NET Core 1.1 或更高版本)
Fedora:fedora-x64、fedora.27-x64、fedora.28-x64(.NET Core 1.1 或更高版本)
Gentoo(.NET Core 2.0 或更高版本):gentoo-x64
openSUSE:opensuse-x64、opensuse.42.3-x64
Oracle Linux:ol-x64、ol.7-x64、ol.7.0-x64、ol.7.1-x64、ol.7.2-x64、ol.7.3-x64、ol.7.4-x64
Red Hat Enterprise Linux:rhel-x64、rhel.6-x64(.NET Core 2.0 或更高版本)、rhel.7-x64、rhel.7.1-x64、rhel.7.2-x64、rhel.7.3-x64(.NET Core 2.0 或更高版本)、rhel.7.4-x64(.NET Core 2.0 或更高版本)
Ubuntu:ubuntu-x64、ubuntu.14.04-x64、ubuntu.16.04-x64、ubuntu.17.10-x64、ubuntu.18.04-x64
Alpine Linux(.NET Core 2.1 或更高版本):alpine-x64、alpine.3.7-x64

3)Mac平台

dotnet publish -c Release -r osx.10.13-x64

其它可选平台,

osx-x64(NET Core 2.0 或更高版本,最低版本为 osx.10.12-x64)、osx.10.10-x64、osx.10.11-x64、osx.10.12-x64(.NET Core 1.1 或更高版本)、osx.10.13-x64

相关文档:https://docs.microsoft.com/zh-cn/dotnet/core/deploying/

推荐文档