我有一个UbuntuCore3.1API,我试图将它托管在ASP.Net下的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我的测试API加载: loaded (/etc/systemd/system/ mytest.service;禁用;供应商预置:已启用) Active: active (运行)自2020年起-11-09 14:15:21 CET;1分钟32s前主要PID: 4212 (MyTestApi)任务: 23 (限制: 18915)内存: 271.4M CGroup: /system.片/mytest.service└─4212└─
11月09 : 14:15:19 HP-全在一-27-xa0xxx systemd1:启动我的测试API.11月09 : 14:15:21 HP-全在一-27-xa0xxx systemd1:启动我的测试API。
但问题是,appsettings.json文件中的配置键具有一个空值:
{
"MyKey": "MyKeyValue",
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"}
键"MyKey“的检索值为空,但我不明白为什么?如果我直接从命令行启动MyTest文件,那么每件事情都是正常的,并且正确地检索了值。
我做错了什么,以前有没有人经历过同样的问题?谢谢你的建议或想法。
发布于 2020-11-10 08:55:18
我发现了造成问题的原因:我需要将工作目录添加到服务文件中,它现在看起来如下所示:
[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键值。
谢谢
https://stackoverflow.com/questions/64752579
复制相似问题