【问题标题】:ASP.Net Core 3.1 API hosted with Systemd on Ubuntu: configuration keys always NULL在 Ubuntu 上使用 Systemd 托管的 ASP.Net Core 3.1 API:配置密钥始终为 NULL
【发布时间】:2020-11-09 13:26:49
【问题描述】:

我有一个 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 文件,一切都很好,并且可以正确检索到值。

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

【问题讨论】:

    标签: ubuntu configuration key systemd asp.net-core-3.1


    【解决方案1】:

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

    [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 键值已正确检索。

    谢谢

    【讨论】:

      猜你喜欢
      • 2021-01-26
      • 1970-01-01
      • 1970-01-01
      • 2019-03-18
      • 2019-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多