我有一个 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 系统中,我创建了位于/etc/systemd/system 目录中的服务文件“mytest.service”:[Unit]
Description=My test API
[Service]
Type=notify
ExecStart=/home/myuser/Desktop/Release/netcoreapp3.1/linux-x64/publish/MyTestApi
[Install]
WantedBy=multi-user.target
然后我使用命令行重新加载了 daemon 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 月 9 日星期一 14:15:21 CET 以来活跃(运行); 1 分钟 32 秒前
主 PID:4212(MyTestApi)
任务:23(限制:18915)
内存:271.4M
CGroup:/system.slice/mytest.service
└─4212/home/myuser/Desktop/Release/netcoreapp3.1/linux-x64/publish/MyTestApi
十一月09 14:15:19 HP-All-in-One-27-xa0xxx systemd[1]:启动我的测试 API...
十一月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 键值已正确检索。谢谢
关于ubuntu - ASP.Net Core 3.1 API 在 Ubuntu : configuration keys always NULL 上由 Systemd 托管,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64752579/