4

ASP.NET Core environments are specified by setting an environment variable named ASPNETCORE_ENVIRONMENT. Visual Studio 2017 uses a launchSettings.json file and Visual Studio Code uses a launch.json file to pass per-session environment variables for debugging.

This way, one can define different profiles for development, staging and production using Visual Studio 2017 and debug without setting a system-wide environment variable.

I want to know if I can incorporate this environment set by launchSettings.json/launch.json into my build process.

  1. MSBuild (.csproj)

    $(ASPNETCORE_ENVIRONMENT) returns empty unless I set a global environment variable. Since building the project is done before launching it, I doubt I can get the environment specified in the launch configuration file.

  2. NPM (package.json)

    I have multiple scripts defined in my package.json file and I would love to pass the current environment to my build scripts, although the previous point applies.

I was using DEV/PROD constants defined in the .csproj file and executing the relevant NPM scripts via <Exec Condition="..." Command="..." /> elements before .NET Core. This way compiling the project with the PROD constant made everything ready for publishing.

I wonder what are the current best practices for such things.

Şafak Gür
  • 7,045
  • 5
  • 59
  • 96
  • You should avoid using the environment during building.The env relates to where the app is launched, in other words, should affect only how an app is running => the same version of the app should work for all available environments (but yes, may work differently) without recompiling. – Set Sep 13 '17 at 08:40
  • Thanks @Set, this makes sense for the MSBuild part. But even when it's not a compile-time constant, I still need to access it somehow from Node.js (to use with gulp/grunt/webpack/etc). And I think I should also be able to test different environments without changing a global environment variable. – Şafak Gür Sep 13 '17 at 10:44
  • Then looks like you don't need to depend on ASPNETCORE_ENVIRONMENT value. Instead, pass custom parameters to scripts that you run. For example for MSBuild look into [How to use custom variables in MS Build scripts?](https://stackoverflow.com/questions/26328431/how-to-use-custom-variables-in-ms-build-scripts) – Set Sep 13 '17 at 12:07
  • But I do depend on the ASP.NET Core environment - that's what the framework gives me when I call `env.IsDevelopment()`. What I'm looking for was a way to access that value on the NPM scripts so I wouldn't have to edit multiple files to change my environment. – Şafak Gür Sep 13 '17 at 14:32
  • I thought I could get away with creating only the assets that are required for the current environment. But I guess I'll need to change my (NPM) build scripts so they create assets for both debug _and_ production environments without any knowledge of which are going to be used (like you said for MSBuild). And just refer to the correct set of files from ASP.NET using the environment tag. – Şafak Gür Sep 13 '17 at 14:33
  • @ŞafakGür, if possible, please check https://github.com/aspnet/Home/issues/724, try to use the `ASPNET_ENV` variable. – Leo Liu Sep 14 '17 at 06:48
  • Hi @Leo, thank you for the suggestion. I found that issue before I posted the question. There are two suggestions there, the first being refering to ASPNET_ENV like you said. But that requires defining a system wide environment variable that needs to be manually updated before testing different environments, independent of the launch profile I selected in Visual Studio. – Şafak Gür Sep 14 '17 at 07:17
  • The second suggestion there, is to open launchSettings.json in the build script (e.g. gulp) and read the environment value from there. The problem with this approach is the said file contains all the launch profiles with no indication of which one is the current one - Visual Studio does not make a visible change in that file to reflect which launch profile I have selected via the launch dropdown. – Şafak Gür Sep 14 '17 at 07:18
  • @ŞafakGür, yes, need to set a system environment variable. It`s ugly and seems could not get the value of $(ASPNETCORE_ENVIRONMENT) before build. – Leo Liu Sep 14 '17 at 07:37

0 Answers0