title: Why NET Core 3.1 BackgroundWorker in ubuntu can't access environment variables?
body: <p>I have a .net core 3.1 background worker that is installed in ubuntu as a background worker. But It can't get the value of the environment variables which I need for multi-configurations that's why as you can see the value of host environment is the default which is Production.</p>
  
  <p>I already tried getting it thru hostenvironment.environmentname and thru Environment.GetEnvironmentVariable.</p>
  
  <p>My Service file in /etc/systemd/</p>
  
  <pre><code>[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
  </code></pre>
  
  <p>Code in NET Core 3.1 background worker.</p>
  
  <pre><code> _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}");
  </code></pre>
  
  <p>Output on cmd.</p>
  
  <p><a href="https://i.stack.imgur.com/srAHd.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/srAHd.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/Z52yU.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Z52yU.png" alt="enter image description here"></a></p>
  
  <p>Code in program.cs</p>
  
  <pre><code> public static IHostBuilder CreateHostBuilder(string[] args) =&gt;
          Host.CreateDefaultBuilder(args)
              .UseSystemd()
              .ConfigureServices((hostContext, services) =&gt;
              {
                  services.AddHostedService&lt;Worker&gt;();
                  var basePath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
                  //NLog.LogManager.LoadConfiguration($"{basePath}/NLog.{hostContext.HostingEnvironment.ContentRootPath}.config");
              })
              .ConfigureAppConfiguration((hostingContext, config) =&gt;
              {
                  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");
              });
  </code></pre>