4

I have managed to successfully publish a ASP.Net Core application and run it as a service on my local machine. What I want to know is how do I publish for my different appsettings.{environment}.json?

I have largely followed these docs for guidance which states that different environments can be set by setting the ASPNETCORE_URLS environment variable but then doesn't elaborate further. I did some digging online and found this stackoverflow thread which seemed promising. I tried to publish with an environment of Development with the following command

dotnet publish -c Release -o G:\Project\publish /p:EnvironmentName=Development

but it still used the appsettings.Production.json by default. The command successfully changed the web.config file to include the following

<environmentVariables>
      <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
</environmentVariables>

But the docs posted above also state that for the web SDK the web.config is unnecessary for a Windows Services app. How do I publish different environments to a Windows Service?

Koobz866
  • 166
  • 1
  • 2
  • 17
  • How do you deploy your app? Azure Dev Ops? Octopus? Manual publish from Visual Studio? PowerShell script? – mason Aug 31 '21 at 16:30
  • 1
    What do you mean with publishing to different environment? Generally speaking, environment variables are useful when running the service, not when publishing it. Is is the same machine or different ones? What stops you from putting `ASPNETCORE_ENVIRONMENT` into the list of environment variables on the host machine? That would allow to switch your environment pretty easily when you run your service – AndrewSilver Aug 31 '21 at 17:50
  • @mason Manual publish from command, just the dotnet cli publish command. I have updated my question to include the exact command. – Koobz866 Sep 01 '21 at 07:37
  • 1
    @AndrewSilver I am attempting this on the same machine. Mainly for education purposes and understanding different deployment methods so I understand this isn't the way you would do it in a production setting. So I understand setting the `ASPNETCORE_ENVIRONMENT` would update it but if I had two services I would like to run on the same machine but with different environments how would I go about that? Wouldn't setting the above set the same environment variable for both services? – Koobz866 Sep 01 '21 at 07:41
  • In a .NET Core app, there is generally no .config file but instead there's an appsettings.json file. In that file, you could have a `"environment": "Production"` setting for example, so it'd know to read one set of variables vs the test variables. Deployment is really up to you, there's so many different ways you can do it. At my day job, we deploy via [Octopus Deploy](https://octopus.com/) so we have that inject the proper values into the configuration files. – mason Sep 01 '21 at 12:56
  • At my side job, I have PowerShell scripts that take an argument indicating the environment, which tells the script which environment variables to look at, it grabs the appropriate variables and injects them into the configuration file before copying them to the server. So really you can do anything you want. Just make sure you automate things - that's really the key. – mason Sep 01 '21 at 12:57

0 Answers0