简体   繁体   中英

How do you compile and run .NET Core Services for Linux?

I'm trying to develop a C# Worker Service with .NET Core 3.1 and run it as a service on an Ubuntu machine.

I found this guide to set up Worker Services on Linux, but I'm getting errors (see screenshot) when trying to start the service.

The guide does not specify how to compile the solution for Linux, so I found this post and compiled a package using dotnet build --runtime ubuntu.18.04-x64 . The result is a folder with my Application and no file extension, as well as a long list of .dll files.

Is this the correct way of publishing Worker Services, or generally .NET Core Applications targeting Linux, on a Windows machine? I wasn't able to find any official documentation or other resources on how to properly do this.

I unfortunately can't directly develop on the Ubuntu machine, as it only has a command-line interface.

在此处输入图片说明

Looking at the documentation for dotnet run ubuntu.18.04-x64 looks like an incorrect runtime: https://docs.microsoft.com/en-us/dotnet/core/rid-catalog

Only common values are listed. For the latest and complete version, see the runtime.json file on the dotnet/runtime repository. Devices running a distribution not listed below may work with one of the Portable RIDs. For example, Raspberry Pi devices running a Linux distribution not listed can be targeted with linux-arm.

  • Portable (.NET Core 2.0 or later versions)

    • linux-x64 (Most desktop distributions like CentOS, Debian, Fedora, Ubuntu, and derivatives)

    • linux-musl-x64 (Lightweight distributions using musl like Alpine Linux)

    • linux-arm (Linux distributions running on ARM like Raspberry Pi)
  • Red Hat Enterprise Linux
    • rhel-x64 (Superseded by linux-x64 for RHEL above version 6)
    • rhel.6-x64 (.NET Core 2.0 or later versions)
  • Tizen (.NET Core 2.0 or later versions)
    • tizen
    • tizen.4.0.0
    • tizen.5.0.0

so instead of dotnet build --runtime ubuntu.18.04-x64 try dotnet build --runtime linux-x64

Following this article did it for me.

The article's example uses "linux-x64" as the dotnet publish runtime identifier , that's what I used (my machine also runs Ubuntu). Until proved the contrary, seems like "linux-x64" may run properly in different distributions.

Am leaving here one question I posted on a related issue I faced during the process of deploying my .net core worker service as a systemd daemon/service unit in linux.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM