ASP.Net Core 3.1 API与Ubuntu上的Systemd一起托管:配置键始终为空

ASP.Net Core 3.1 API与Ubuntu上的Systemd一起托管:配置键始终为空,ubuntu,configuration,key,systemd,asp.net-core-3.1,Ubuntu,Configuration,Key,Systemd,Asp.net Core 3.1,我有一个ASP.NETCore3.1API,我试图在Ubuntu下的systemd中托管它。我将NuGet软件包Microsoft.Extensions.Hosting.Systemd添加到项目中,并按预期在program.cs文件中使用.Usesystemd()函数。此外,我使用命令行创建了一个自包含服务来发布API: dotnet publish -c Release -r linux-x64 --self-contained 发布文件夹被复制到Ubuntu系统中,我创建了服务文件“myt

我有一个ASP.NETCore3.1API,我试图在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) 内存:27140万 CGroup:/system.slice/mytest.service └─4212/home/myuser/Desktop/Release/netcoreapp3.1/linux-x64/publish/MyTestApi

11月9日14:15:19 HP-All-in-One-27-xa0xxx systemd[1]:正在启动我的测试API。。。 11月9日14:15:21 HP-All-in-One-27-xa0xxx systemd[1]:启动了我的测试API

但问题是,appsettings.json文件中的配置密钥具有空值:

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

检索到的键“MyKey”的值为空,但我不明白为什么? 如果我从命令行直接启动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键值已被正确检索

谢谢