如何在Windows机器上编译Linux的.NET Core应用程序

jav*_*iry 11 c# linux .net-core asp.net-core-webapi

我正在Windows 10计算机上开发.NET Core应用程序(使用Visual Studio 2015更新3 + Microsoft .NET Core 1.0.1 VS 2015工具预览2),该应用程序应该在Ubuntu 16计算机上发布.要做到这一点,我必须将我的源代码移动到最终机器并在那里编译,以使其运行.例如,我无法在Windows上编译代码并在Linux上运行它.问题:有没有办法在win机器上编译代码并在linux上运行它?

Set*_*Set 28

使用dotnet build命令,您可以指定--runtimeflag

-r | --runtime <RUNTIME_IDENTIFIER>

目标运行时构建.有关可以使用的运行时标识符(RID)的列表,请参阅RID目录.

代表具体操作系统的RID通常遵循这种模式 [os].[version]-[arch]

例如,为Ubuntu 16.04运行时使用构建项目及其依赖项:

dotnet build --runtime ubuntu.16.04-x64
Run Code Online (Sandbox Code Playgroud)

  • 谢谢。但我收到此错误:“找不到与目标运行时之一兼容的框架 '.NETCoreApp,Version=v1.0' 的运行时目标:'win10-x64、win81-x64、win8-x64、win7-x64” '.` (2认同)
  • 什么时候应该使用这个运行时标志?它是一个适用于所有用例的通用标志,只是为了优化特定平台生成的二进制文件吗?具体来说,它仅用于构建独立的二进制文件,还是也可以用于构建在 dotnet core SDK/运行时上运行的应用程序? (2认同)

Ale*_*lex 12

选项 1:命令行

dotnet build ProjectFile.csproj --runtime linux-x64
Run Code Online (Sandbox Code Playgroud)

适用于 Linux、Windows 和 Mac。

选项 2:Visual Studio

如果您愿意,还可以在 Visual Studio 中“发布”您的应用程序。选择“文件系统”发布方法并设置此设置:

在此输入图像描述


Was*_*ter 8

dotnet publish **path to your solution** --configuration Release --framework netcoreapp3.0 --output .**output path** --self-contained false --runtime linux-x64 --verbosity quiet
Run Code Online (Sandbox Code Playgroud)

  • 这与右键单击项目,发布 =&gt; 将框架设置到 Linux 并发布它相同吗? (2认同)

Rog*_*god 6

对于现在看到这不再起作用的任何人,似乎从 2020 年 11 月 10 日的更新开始,您现在必须指定项目文件,因为它不再喜欢在解决方案 (.sln) 上使用指定的运行时。

这里提出了一个关于此的问题(https://github.com/dotnet/sdk/issues/14281),但显然这不会立即得到解决。

所以以前这会起作用:

dotnet build --runtime ubuntu.xx.xx-x64
Run Code Online (Sandbox Code Playgroud)

它现在想要这样的东西:

dotnet build ProjectName.csproj --runtime ubuntu.xx.xx-x64
Run Code Online (Sandbox Code Playgroud)