2

This doesn't seem to work in a core console app:

dotnet publish myproject.csproj /p:EnvironmentName=MyEnvironment

Is there an alternative for publishing a console app that alters the environment name variable programmatically?

user441365
  • 3,934
  • 11
  • 43
  • 62
  • 1
    What were you expecting it to do? publishing only creates artefacts (which you copy/push to your web server or hosting platform). Environment Variables are evaluated when the application runs, not when its published. You just set the variable in your publishing environment (Azure App Service, Docker container, IIS, or your linux/windows host depending on where you hit) – Tseng Feb 22 '19 at 08:14
  • I mean, it makes no sense to recompile an application just to change environment, thats the whole point of the `ASPNETCORE_ENVIRONMENT` (see docs: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/environments?view=aspnetcore-2.2). How you set it depends on your target environment, on linux you can do it via `ASPNETCORE_ENVIRONMENT=Development dotnet run ./MyApp.dll` etc. then its just valid for the one execution command. See the link for all ways to set it – Tseng Feb 22 '19 at 08:17
  • 1
    you can publish a web app and alter the environment variable with the command above so I am wondering how this could be done in a console app. If you don't want to help you don't need to post anything. – user441365 Feb 22 '19 at 08:39
  • You are not even providing information on which target/hosting platform you intend to deploy in the first place. If you'd took the time to read the provided link, you'd realized that every target platform requires you to set the environment variable on the target hosting system, with the exception of _For Windows IIS deployments_, which you do via `.pubxml` file. To override project properties (namely `EnvironmentName`, you'd need to use `dotnet msbuild ... /p:EnvironmentName=Something`). Its still counter intuitive and not the way it was meant to set environment :P – Tseng Feb 22 '19 at 08:54
  • "...with the exception of For Windows IIS deployments...". Hence my question. Yes it is a Windows IIS deployment and yes I need to override the project property for the environment. If you'd took the time to actually read my question it says that "dotnet msbuild ... /p:EnvironmentName=Something" does not work for console apps :P – user441365 Feb 22 '19 at 09:00
  • Sorry its `-p:`, not `/p:`, copy & paste error, but you have to use `dotnet msbuild` (`dotnet build` should work too) not `dotnet publish`, as later one doesn't have this parameter. Also for the future: Provide sufficient information, if you want get more accurate answers – Tseng Feb 22 '19 at 09:48
  • "dotnet msbuild" does not publish. It doesn't have the same behaviour. I am using the "dotnet publish" command in my question in a web app and it works perfectly. – user441365 Feb 22 '19 at 10:44
  • Maybe have a look at the `-t` parameter??! [See the docs](https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-msbuild) – Tseng Feb 22 '19 at 12:47
  • ok so you were right in that it publishes, but it still doesn't alter the environment variable, which is the issue here. – user441365 Feb 22 '19 at 13:01

1 Answers1

1

Config in ASP.NET Core is entirely externalized. As such there's nothing you can do as part of the build (or therefore publish) that will set the environment. The same code can be published multiple different places, all using different values for environment. In other words, the environment is a function of the destination, not the act of publishing.

As part of a CI/D pipeline, you can easily set environment variables, but importantly, the dotnet publish command is not a CI/D pipeline. This would mean setting up something in Pipelines in Azure DevOps, for example. Or, if you don't want to go quite that far (though, you really should), you'll want to create a PowerShell script perhaps that will handle publishing and setting environment variables appropriately.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • So it can't be done. I'll create a PowerShell script as suggested to alter the app.config then. Thanks – user441365 Feb 25 '19 at 09:14
  • interesting. so I how do I safeguard production connection string for example? previously I wouldn't include it in git and would publish using Release configuration. – Toolkit Aug 02 '19 at 07:47
  • Secrets of any sort need to go into config that is not part of the project at all (i.e. environment variables) or ideally in something that is encrypted at rest (like KeyVault). – Chris Pratt Aug 02 '19 at 11:16