You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET 6.0 Linux systemd服务启动时出现Exec Format Error问题求助

.NET 6自包含部署Linux systemd服务启动失败(Exec format error)解决方案

问题场景

  • 环境:.NET 6 Minimal API,已在Program.cs中配置跨平台服务适配,代码如下:
if (Debugger.IsAttached == false)
{
    if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
    {
        builder.Host.UseWindowsService();
        builder.WebHost.ConfigureKestrel(serverOptions =>
        {
            int port = 1000;
            int.TryParse(config.GetValue<string>("MicroServicePort"), out port);
            serverOptions.ListenAnyIP(port, listenOptions =>
            {
                listenOptions.Protocols = HttpProtocols.Http1AndHttp2;
                listenOptions.UseHttps(config.GetValue<string>("WindowsCertificatePath"));
            });

        });
    }
    if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
    {
        builder.Host.UseSystemd();
        builder.WebHost.ConfigureKestrel(serverOptions =>
        {
            int port = 1000;
            int.TryParse(config.GetValue<string>("MicroServicePort"), out port);
            serverOptions.ListenAnyIP(port, listenOptions =>
            {
                listenOptions.Protocols = HttpProtocols.Http1AndHttp2;
                listenOptions.UseHttps(config.GetValue<string>("LinuxCertificatePath"), config.GetValue<string>("CertificatePassword"));
            });

        });
    }
}
  • 部署方式:Visual Studio自包含部署,目标RedHat Linux服务器(x86_64架构,系统版本:Linux LoadTesting 4.18.0-305.el8.x86_64 #1 SMP Thu Apr 29 08:54:30 EDT 2021 x86_64 x86_64 x86_64 GNU/Linux)未安装.NET运行时
  • systemd服务配置文件(/etc/systemd/system/WebAPI.service):
[Unit]
Description=WebAPI
StartLimitIntervalSec=0

[Service]
Type=simple
Restart=on-failure
RestartSec=60
User=johndoe
ExecStart=/usr/local/bin/Microservices/LinuxServices/WebAPI/Publish/WebAPI.dll

[Install]
WantedBy=multi-user.target
  • 启动报错日志(journalctl | grep WebAPI):
Feb 03 02:39:42 LoadTesting systemd[1]: Stopped WebAPI.
Feb 03 02:39:42 LoadTesting systemd[1]: Started WebAPI.
Feb 03 02:39:42 LoadTesting systemd[14975]: WebAPI.service: Failed to execute command: Exec format error
Feb 03 02:39:42 LoadTesting systemd[14975]: WebAPI.service: Failed at step EXEC spawning /usr/local/bin/Microservices/LinuxServices/WebAPI/Publish/WebAPI.dll: Exec format error
Feb 03 02:39:42 LoadTesting systemd[1]: WebAPI.service: Main process exited, code=exited, status=203/EXEC
Feb 03 02:39:42 LoadTesting systemd[1]: WebAPI.service: Failed with result 'exit-code'.
Feb 03 02:40:42 LoadTesting systemd[1]: WebAPI.service: Service RestartSec=1min expired, scheduling restart.
Feb 03 02:40:42 LoadTesting systemd[1]: WebAPI.service: Scheduled restart job, restart counter is at 47.
Feb 03 02:40:42 LoadTesting systemd[1]: Stopped WebAPI.

核心问题与解决方案

1. 修正可执行文件路径

自包含部署在Linux下生成的是无.dll后缀的可执行文件,无法直接运行.dll文件。修改systemd服务文件的ExecStart:

[Service]
# 移除.dll后缀,指向正确的可执行文件
ExecStart=/usr/local/bin/Microservices/LinuxServices/WebAPI/Publish/WebAPI

2. 添加执行权限

确保可执行文件具备执行权限,执行以下命令:

chmod +x /usr/local/bin/Microservices/LinuxServices/WebAPI/Publish/WebAPI

3. 验证发布架构匹配

检查Visual Studio发布配置是否选择了正确的目标架构:

  • 发布时,目标运行时需设置为linux-x64(对应x86_64架构的Linux服务器)
  • 若架构不匹配,重新发布并上传文件

4. 检查文件上传模式

使用WinSCP上传时,需选择二进制传输模式,避免文本模式损坏可执行文件:

  • 在WinSCP中,点击传输->传输设置,选择二进制模式后重新上传

5. 重启服务并验证

修改配置后,重新加载systemd并重启服务:

sudo systemctl daemon-reload
sudo systemctl restart WebAPI

查看服务状态:

sudo systemctl status WebAPI

查看实时运行日志:

journalctl -u WebAPI -f

内容的提问来源于stack exchange,提问作者TriedAtLeast

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 13:35:18