У меня есть фоновый работник. net core 3.1, который установлен в ubuntu как фоновый работник. Но он не может получить значение переменных среды, которое мне нужно для мультиконфигураций, поэтому, как вы можете видеть, значением хост-среды по умолчанию является Production.
Я уже пытался получить его через хост-среду .environmentname и thru Environment.GetEnvironmentVariable.
Файл моей службы в / etc / systemd /
[Unit]
Description=TestService
[Service]
Type=notify
WorkingDirectory=/home/centos/TestService/
ExecStart=/usr/bin/dotnet /home/centos/TestService/WorkerService2.dll
User=centos
[Install]
WantedBy=multi-user.target
Код в NET Фоновый рабочий Core 3.1.
_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
_logger.LogError("ERROR testing");
_logger.LogInformation($"Hosting Enviornment: {_hostEnvironment.EnvironmentName}");
_logger.LogInformation(_configuration["Test"]);
var basePath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
var contentroothpath = _hostEnvironment.ContentRootPath;
Console.WriteLine($"basepath = {basePath}");
Console.WriteLine($"contentrootpath = {contentroothpath}");
_logger.LogInformation($"basepath = {basePath}");
_logger.LogInformation($"contentrootpath = {contentroothpath}");
var environmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
var environmentName2 = Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT");
_logger.LogInformation($"ASPNETCORE_ENVIRONMENT = {environmentName}");
_logger.LogInformation($"DOTNET_ENVIRONMENT = {environmentName2}");
Вывод на cmd.


Код в program.cs
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseSystemd()
.ConfigureServices((hostContext, services) =>
{
services.AddHostedService<Worker>();
var basePath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
//NLog.LogManager.LoadConfiguration($"{basePath}/NLog.{hostContext.HostingEnvironment.ContentRootPath}.config");
})
.ConfigureAppConfiguration((hostingContext, config) =>
{
config.AddJsonFile(
$"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", optional: true, reloadOnChange: true);
//NLog.LogManager.LoadConfiguration($"/home/centos/TestService/{hostingContext.HostingEnvironment.EnvironmentName}.config");
//NLog.LogManager.LoadConfiguration($"{basePath}" +
// $"/NLog.{hostingContext.HostingEnvironment.EnvironmentName}.config");
});