Monday, November 04, 2024

Using multiple environments in ASP.NET Core

ASP.NET Core configures app behavior based on the runtime environment using an environment variable.

IHostEnvironment.EnvironmentName can be set to any value, but the following values are provided by the framework:

  • Development : The launchSettings.json file sets ASPNETCORE_ENVIRONMENT to Development on the local machine.
  • Staging
  • Production : The default if DOTNET_ENVIRONMENT and ASPNETCORE_ENVIRONMENT have not been set.

When comparing appsettings.development.json and appsettings.json, the key difference lies in their deployment environments. appsettings.development.json is typically used for development and testing environments, whereas appsettings.json is used for production environments.

The .development.json file contains sensitive information such as database credentials and API keys, which are not committed to source control and are generated locally. In contrast, appsettings.json contains non-sensitive configuration settings that are committed to source control and used in production.

Here is how this con be done in Program.cs file

public class Program
    {
        public static void Main(string[] args)
        {
            BuildWebHost(args).Run();

        }

        public static IWebHost BuildWebHost(string[] args) =>
         WebHost.CreateDefaultBuilder(args)
           .UseStartup<Startup>()
           .ConfigureAppConfiguration((context, config) =>
           {
               var env = context.HostingEnvironment;
               config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                     .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
           })
           .Build();
    }
  

Here is sample appsettings.json file

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "ApiSettings": {
  },
  "AllowedHosts": "*",
  "isLocal": "1",
  "Email": {
  },
  "LanguageService": {
  }
}
  

These appsettings.json or appsettings.staging.json or appsettings.production.json can be set from launchSettings.json

Here is how it looks

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:44459",
      "sslPort": 44393
    }
  },
  "profiles": {
    "Client.PWA": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "http://localhost:5198",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development" //Development//Staging//Production
      }
    }
  }
}
  

ASPNETCORE_ENVIRONMENT in above launchsettings.json will determine which configuration it needs to pick. In above case, its looking for Development settings, here I have Staging and Production configured as well.

This approach helps maintain secure practices while allowing for different configuration settings between environments.

No comments:

Post a Comment