Setting ASPNET_ENV using DNX commands when deploying ASP.NET Core apps to IIS

Reason

Setting ASPNET_ENV as a system wide environment variable is not an option when dealing with webservers which host multiple ASP.NET 5 sites spread across various ASP.NET environments, as each site potentially requires a distinct ASPNET_ENV value.

Although setting ASPNET_ENV for each individual application pool is possible, I found the approach tedious and obscure when applied to servers hosting many sites, which is still the norm at my workplace (e.g. servers hosting a mixture of test and quality assurance sites).

Hence, when dealing with multiple ASP.NET environments on a single server, using DNX commands to set the ASPNET_ENV variable at run time can be a simple alternative to creating a dedicated user account for each application pool and then having to manage the ASPNET_ENV environment variable for each of those accounts.

Note: Alternatives include using launchSettings.json or another of the myriad ways to set an environment variable.

Code

The project.json file shown below contains three commands, one for each environment the application will be run in.

Each command sets the ASPNET_ENV to whatever value is required for that particular instance of the application, using a command line parameter.

The command Development is used when developing locally (surprise) and when running integration tests on a build server, while Staging and Production are used for IIS sites.

{
  "dependencies": {
    "Microsoft.AspNet.IISPlatformHandler": "1.0.0-beta8",
    "Microsoft.AspNet.Server.Kestrel": "1.0.0-beta8",
    "Microsoft.AspNet.Hosting": "1.0.0-beta8", 
    [...]
  },

  "commands": {
    "development": "Microsoft.AspNet.Hosting --ASPNET_ENV Development --server=Microsoft.AspNet.Server.Kestrel --server.urls=http://localhost:123456",
    "staging": "Microsoft.AspNet.Server.Kestrel --ASPNET_ENV Staging",
    "production": "Microsoft.AspNet.Server.Kestrel --ASPNET_ENV Production"
  },

  "scripts": {
    "postrestore": [
      "echo After restoring packages %project:Name%",
      "npm install",
      "gulp default --minify"
    ]
  },

  "configurations": {
    "Debug": {
      "compilationOptions": {
        "define": ["DEBUG", "TRACE"]
      }
    },
    "Test": {
      "compilationOptions": {
        "define": ["DEBUG", "TRACE"]
      }
    },
    "Release": {
      "compilationOptions": {
        "define": ["RELEASE", "TRACE"],
        "optimize": true
      }
    }
  },

  [...]
}

To make IIS call our “environment specific commands” instead of defaulting to web, the --iis-command command line option must be provided when publishing/packaging the application via dnu publish, as exemplified below.

dnu publish --configuration Test --iis-command Staging --out "../../artifacts/Test" --runtime dnx-clr-win-x64.1.0.0-beta8 --no-source --quiet

Example

Example dnu publish output and the resulting Web.config file, configured to start up using the Staging command.

DNU publish

DNU publish

Web.config

Web.config

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s