【问题标题】:.Net Core on Linux runs fine with 'dotnet myapp.dll' but fails with systemctlLinux 上的 .Net Core 使用“dotnet myapp.dll”运行良好,但使用 systemctl 失败
【发布时间】:2019-02-16 06:52:36
【问题描述】:

我正在尝试按照this tutorial 中的说明在 Ubuntu Server 16.04 LTS 上托管 ASP.Net Core MVC 应用程序。

在 Linux 机器上,我使用 git pull 拉入我的项目。我使用dotnet build 成功构建了它,它触发了包还原。我使用dotnet publish -c Release -r linux-x64 成功发布它。我将目录更改为bin/Release/netcoreapp2.1 并运行sudo cp ./*.* /var/aspnetcore/myapp/ 将文件复制到我托管项目的目录中。我导航到该目录并输入dotnet myapp.dll - 应用程序开始侦听端口 5000 和 5001,然后从另一台计算机上,我可以在浏览器中输入我的域名并查看网站,这意味着 Nginx 反向代理和 Kestrel一定可以正常工作。

现在我想将应用程序作为服务运行,以便它在计算机上启动、在崩溃时重新启动、记录错误等,但是当我输入 sudo systemctl start kestrel-myapp.service 时它会立即崩溃并尝试每 10 秒重新启动一次但失败。日志显示错误:An assembly in the application dependencies manifest (myapp.deps.json) was not found: package: 'Microsoft.EntityFrameworkCore.Relational.Design', version '1.1.5' path: 'lib/netstandard1.3/Microsoft.EntityFrameworkCore.Relational.Design.dll' Main process exited, code=exited, status=140/n/a

所以,1.1.5 版本看起来有点低,但我更新了所有的 NuGet 包,它仍然想使用它。另外,如果依赖项有问题,我不明白为什么dotnet myapp.dll 不会崩溃。有人知道如何解决这个问题吗?

这是我的服务文件:

[Unit]
Description=myapp ASP.Net Core MVC Application

[Service]
WorkingDirectory=/var/aspnetcore/myapp
ExecStart=/usr/bin/dotnet /var/aspnetcore/myapp/myapp.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
SyslogIdentifier=myapp
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

[Install]
WantedBy=multi-user.target

额外问题:我应该将我的 wwwroot 文件夹与我的静态文件复制到哪里?应用程序运行但找不到我的任何 javascript 或 css 文件,我不确定它们属于哪里。

【问题讨论】:

  • 顺便说一下,文件夹/lib/netstandard1.3不存在。

标签: linux ubuntu asp.net-core .net-core


【解决方案1】:

确保 WorkingDirectory 设置为运行时 dll 文件所在的目录。

【讨论】:

    【解决方案2】:

    当您dotnet publish -c Release -r linux-x64 时,它会创建多个目录。您想使用bin/Release/netcoreapp2.1/linux-x64/publish 目录。其他目录也包含构建,但这些构建假定它们将在开发系统上运行。只有publish 目录包含应该部署的位。其他目录包含假定它们在开发环境中运行的位。

    当您运行 dotnet myapp.dll 时它可以工作,因为您仍然以普通用户身份运行,并且它会看到您的本地开发资产,包括 nuget 缓存。

    当您通过systemctl 运行时,它以root 运行,它没有任何本地nuget 缓存,并且不能使用您的代码的开发(非published)版本。

    【讨论】:

    • 耶! /linux-x64/publish 中有更多文件。谢谢您的帮助!你知道是否可以将 wwwroot 复制到 /var/aspnetcore/myapp 中,或者这些文件属于哪里?我习惯于从 VS 发布到 IIS,它为您完成所有这些...
    • 阅读文档,publish 应该已经包含wwwrootdocs.microsoft.com/en-us/aspnet/core/host-and-deploy/…。这不会发生在你身上吗?
    • 当我做对时它确实如此......复制时我没有使用-a,所以我没有递归复制。这解决了问题。抱歉,我对 bash 非常了解,可以按照教程进行操作,但我对 Linux 不是很有经验。再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-28
    相关资源
    最近更新 更多