Search code examples
console-application.net-6.0environment

How does .Net 6 determine its Enviroment


I've created a .Net 6.0 Console application. I need it to be able to migrate the database using Entity Framework. Now when I run it, it says its environment is 'Production'. Where is that configured?

enter image description here

Here's the startup code for the application.

    static void Main(string[] args)
    {
        using IHost host = Host.CreateDefaultBuilder(args)
            .UseSerilog((context, loggerConfiguration) => loggerConfiguration
                .ReadFrom.Configuration(context.Configuration)
                .WriteTo.Console())
            .ConfigureServices((context, services) =>
            {
                services.RegisterCoreServices(context.Configuration);
            })
            .Build();

        CallBatch(host.Services, args);

        host.RunAsync();
    }

Solution

  • Production is the default.

    Check out Use multiple environments in ASP.NET Core

    Environments

    To determine the runtime environment, ASP.NET Core reads from the following environment variables:

    1. DOTNET_ENVIRONMENT
    2. ASPNETCORE_ENVIRONMENT when the WebApplication.CreateBuilder method is called. The default ASP.NET Core web app templates call WebApplication.CreateBuilder. The ASPNETCORE_ENVIRONMENT value overrides DOTNET_ENVIRONMENT.

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

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