liveBook · Manning

Environment

As explained in ASP.NET Core in Action, 3e

A hosting environment is the runtime context that identifies where an application is running and determines environment-specific configuration and behavior.

A hosting environment tells an ASP.NET Core application where it is running so it can choose suitable configuration and behavior. A development machine might use a local database and detailed logs; production might use a different database and fewer logs. The application can make those choices without being recompiled. section 10.0 section 10.4

ASP.NET Core reads the ASPNETCORE_ENVIRONMENT environment variable at startup. If it is not set, the environment defaults to Production. The current environment is available through IHostEnvironment, including builder.Environment. Prefer IsDevelopment(), IsStaging(), IsProduction(), or IsEnvironment(name) to comparing EnvironmentName strings directly: these methods match without regard to case. section 10.4.1 section 10.4.3

Listing 10.13 Adding environment-specific appsettings.json files
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
 
IHostEnvironment env = builder.Environment;           #1
 
builder.Configuration.Sources.Clear();
builder.Configuration
    .AddJsonFile(
        "appsettings.json",
        optional: false)                              #2
    .AddJsonFile(                                     #3
        $”appsettings.{env.EnvironmentName}.json”,    #3
        Optional: true);                              #3
 
WebApplication app = builder.Build();
 
app.MapGet("/", () =>"Hello world!");
 
app.Run();
#1 The current IHostEnvironment is available on WebApplicationBuilder.
#2 It’s common to make the base appsettings.json compulsory.
#3 Adds an optional environment-specific JSON file where the filename varies with the environment

See how a base appsettings.json file can be followed by an optional file named for the current environment. The listing clears configuration sources to show the setup explicitly; the default WebApplicationBuilder configuration already supports environment-specific files. section 10.4.2 section 10.4.1

The environment can also change the middleware pipeline. For example, development can log request details, while staging and production can use exception-handling middleware. For application services, prefer injecting strongly typed settings supplied by configuration providers rather than injecting IHostEnvironment and branching on it throughout the code. section 3.7 section 10.4.2

To test locally, set ASPNETCORE_ENVIRONMENT in a launchSettings.json profile; profiles keep variables specific to a project or run profile. You can also select an environment when running with dotnet run. Check the environment logged at startup to confirm which value the application picked up. section 10.4.3

How ASP.NET Core in Action, 3e covers it

How deep it goes
Walks through identifying an environment, loading its configuration, and setting it locally, without examining hosting internals.
Examples
Uses separate database settings, different logging levels, environment-specific appsettings.json files, and conditional exception handling.
What it assumes
Assumes a basic ASP.NET Core minimal API and introduces hosting environments through its configuration workflow.
What you can do after
Operate/configure it by setting an environment, checking it through IHostEnvironment, and selecting environment-specific settings or middleware.
How it connects
Connects hosting environments to configuration providers, strongly typed options, User Secrets, and middleware.

Also explained in

Compare how 8 books explain environment →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.