title: Setting environment variables for dotnetcore Windows service -- process only, not system or user
body: <p>Our developers are writing some Windows Services in .NET Core 3.1.</p>
  <p>As I understand it, the typical pattern for Core development is to check the DOTNET_ENVIRONMENT variable to switch on different configurations.</p>
  <p>For Reasons<sup>TM</sup>, we want to avoid setting system-level or even user-level machine environment variables. Some of the reasons are:</p>
  <ul>
  <li>something else to set up when rolling out a new server</li>
  <li>might want to run different &quot;environments&quot; on the same server -- not for services, maybe, but for other commandline apps</li>
  </ul>
  <p>For Kestrel hosted apps launched by IIS, extra environment values can be set in web.config. This can't apply to commandline apps.</p>
  <p>What's the best practice for this? Is there a way to set environment variables before launching the executable? Or set environment variables within the process, at startup time, by reading them from a config file?</p>
  <p>Other things to mention:</p>
  <ul>
  <li>When we start containerizing things, setting container-level environment variables is obviously the way to go, but for shared Windows servers (actually VMs) like this, we need a different approach.</li>
  <li>We have reviewed <a href="https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-3.1" rel="noreferrer">https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-3.1</a> . Even though it is targeted at ASP.NET Core and not console/service apps in Core.</li>
  </ul>