How to default to not use appsettings.Production in .Net

22 Views Asked by At

I want to default to using test server settings and only use production settings if it´s a production server. I'm having issues both that it completely ignores DOTNET_ENVIRONMENT variable and always uses appsettings.Production.json even if I manually tell it otherwise.

appsettings.json

}
  "EnvTest": "Test"
}

appsettings.Production.json

}
  "EnvTest": "Production"
}

Program.cs

using Serilog;
using System.Runtime.InteropServices;



var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddWindowsService(options =>
{
    options.ServiceName = "test";
});


builder.Environment.EnvironmentName = Environment.GetEnvironmentVariable("DOTNET_Environment") ?? "Default";

// If Production server overwrite default settings with production settings.
if(builder.Environment.IsProduction())
{
    builder.Configuration.AddJsonFile("appsettings.Production.json", optional: false, reloadOnChange: true);
}

Log.Information($"Starting service as {builder.Environment.EnvironmentName} with setting {builder.Configuration.GetValue<string>("EnvTest")} on {RuntimeInformation.FrameworkDescription}");

var host = builder.Build();
host.Run();

On server with set i get DOTNET_ENVIRONMENT="Test"

Output:

Starting service as Defaulted with setting Production on .NET 8.0.3

How can i convince the app to not use production settings?

Tried both setting DOTNET_ENVIRONMENT variable and manually set builder.Environment.EnvironmentName

0

There are 0 best solutions below