在Ubuntu上使用Systemd托管的ASP.Net Core 3.1 API:配置密钥始终为NULL



我有一个ASP.Net Core 3.1 API,我试图在Ubuntu下的systemd中托管它。我向项目中添加了NuGet包Microsoft.Extensions.Hosting.Systemd,并按预期在program.cs文件中使用了.Usesystemd((函数。此外,我使用命令行创建了一个自包含的服务来发布API:

dotnet publish -c Release -r linux-x64 --self-contained

发布文件夹被复制到Ubuntu系统中,我创建了服务文件mytest.service,它位于/etc/systemd/system目录中:

[Unit]
Description=My test API
[Service]
Type=notify
ExecStart=/home/myuser/Desktop/Release/netcoreapp3.1/linux-x64/publish/MyTestApi
[Install]
WantedBy=multi-user.target

然后,我使用命令行重新加载了守护进程conf:

sudo systemctl daemon-reload

我试着启动正确启动的服务:

sudo systemctl start mytest.service

状态显示服务正在运行:

sudo systemctl status mytest.service

●mytest.service-我的测试API已加载:已加载(/etc/systemd/system/mytest.service;已禁用;供应商预设:已启用(活动:自2020-11-09周一14:15:21 CET起活动(跑步(;1分32秒前主PID:4212(MyTestApi(任务:23(限制:18915(内存:2.714亿C组:/system.slice/ytest.service└─4212/home/myuser/Desktop/Release/netcoreapp3.1/linux-x64/publish/MyTestApi

11月。09 14:15:19 HP-All-in-One 27-xa0xxx systemd[1]:正在启动我的测试API。。。nov.09 14:15:21 HP-All-in-One 27-xa0xxx systemd[1]:开始我的测试API。

但问题是appsettings.json文件中的配置密钥有一个NULL值:

{
"MyKey": "MyKeyValue",
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"

}

检索到的密钥"的值;MyKey";是NULL,但我不明白为什么?如果我从命令行直接启动MyTest文件,那么一切都很好,并且值被正确检索。

我做错了什么?以前有人遇到过同样的问题吗?谢谢你的任何建议或想法。

我发现了问题的原因:我需要将工作目录添加到服务文件中,现在看起来如下:

[Unit]
Description=My test API
[Service]
Type=notify
WorkingDirectory=/home/myuser/Desktop/Release/netcoreapp3.1/linux-x64/publish
ExecStart=/home/myuser/Desktop/Release/netcoreapp3.1/linux-x64/publish
/MyTestApi
[Install]
WantedBy=multi-user.target

现在,appsettings.json键值已正确检索。

感谢

最新更新