简体   繁体   中英

Kestrel don't see VPS environment variable. Deployment .net core 3.1 app with apache and kestrel

I'm trying to publish my web app in .net core with using the environment variable on VPS.

Firstly I've launched my app in develop mode when I'm using secret.json (connection string, API key, etc) and everything works well. That's means apache, kestrel and app work correctly.

But at least I want my app used environment variable when I want to save connection string etc.

This is my secret.json:

{
  "ConnectionStrings": {
    "DefaultConnection": "...",

  },
  "CloudinarySettings": {
    "CloudName": "...",
    "ApiKey": "...",
    "ApiSecret": "..."
  }
}

This is my startup.cs class when I configure choice from my app has to load credentials.

  public Startup(IWebHostEnvironment env)
    {
        var config = new ConfigurationBuilder();

        if (env.IsDevelopment())
        {
            config.AddJsonFile("secrets.json");

        }

        if (env.IsProduction())
        {
            config.AddEnvironmentVariables("ASPNETCORE_CMS_");
        }

        Configuration = config.Build();
    }

This is my VPS.bashrc file when I globally saved environment variable

export ASPNETCORE_CMS_ConnectionStrings__DefaultConnection='...'
export ASPNETCORE_CMS_CloudinarySettings__CloudName=...
export ASPNETCORE_CMS_CloudinarySettings__ApiKey=...
export ASPNETCORE_CMS_CloudinarySettings__ApiSecret=...

etc.. 

And this variables saved correctly, because when I tested this, VPS return my correct value:

echo $ASPNETCORE_CMS_ConnectionStrings__DefaultConnection

Firstly, I'm trying only to use this variable but my app didn't see it's. I'm read in docs that I need to parse this variable into my kestrel service configuration. This is my config file:

[Unit]
Description=Starting app

[Service]
WorkingDirectory=/var/CMS
ExecStart=/usr/bin/dotnet /var/CMS/CMS.dll
Restart=always
RestartSec=10
SyslogIdentifier=cms
User=ubuntu

Environment=ASPNETCORE_ENVIRONMENT=Production // <- this works
Environment=ASPNETCORE_CMS_ConnectionStrings__DefaultConnection="..." <- this not
Environment=ASPNETCORE_CMS_CloudinarySettings__CloudName=...
Environment=ASPNETCORE_CMS_CloudinarySettings__ApiKey=...
Environment=ASPNETCORE_CMS_CloudinarySettings__ApiSecret=....

[Install]
WantedBy=multi-user.target

...and kestrel return me an error, because it can't connect with the database. This means it can't read this env variable.

The rest of my app is ok, because like I said before I tested in dev mode and everything was ok.

EDIT:

As suggested, I checked that this process has this environemt and everythink is ok

This is my process:

ubuntu   19208  0.1  4.3 2994328 86580 ?       SLsl 06:56   0:01 /usr/bin/dotnet /var/CM/CMS.dll

this is result of this PID:

ubuntu@vps:~$ cat /proc/19208/environ | tr '\0' '\n'
LANG=en_US.UTF-8
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin
HOME=/home/ubuntu
LOGNAME=ubuntu
USER=ubuntu
SHELL=/bin/bash
ASPNETCORE_ENVIRONMENT=Production
ASPNETCORE_CMS_ConnectionStrings__DefaultConnection="..."
ASPNETCORE_CMS_CloudinarySettings__CloudName=....
ASPNETCORE_CMS_CloudinarySettings__ApiKey=....
ASPNETCORE_CMS_CloudinarySettings__ApiSecret=...

ubuntu@vps:~$

Does anyone see a solution?

According to docs using ASPNETCORE_ for custom params is forbidden:

The default configuration loads environment variables and command line arguments prefixed with DOTNET_ and ASPNETCORE_. The DOTNET_ and ASPNETCORE_ prefixes are used by ASP.NET Core for host and app configuration, but not for user configuration.

I suppose builder overrides AddEnvironmentVariables("ASPNETCORE_CMS_"); with smth like AddEnvironmentVariables("ASPNETCORE_"); so your variable becomes CMS_ConnectionStrings__DefaultConnection or similar. Use another prefix for your settings

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM