Page 1 of 1

Lösung der Symbolfiguration mit umgebungsspezifischen Appsettings.json

Posted: 27 Feb 2025, 11:06
by Anonymous
Ich habe zwei Einstellungsdateien AppSettings.json und AppSettings.development.json.

Code: Select all

if (Debugger.IsAttached)
{
configBuilder
.SetBasePath(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location))
.AddJsonFile("appsettings.Development.json", optional: false, reloadOnChange: true);
}
else
{
configBuilder
.SetBasePath(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location))
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
}

IConfiguration config = configBuilder.Build();
< /code>
Ich erhalte die richtigen Werte von appettings.development.json Wenn ich von
config.section("SectionName ") im Programm lese. class = "Lang-CS PrettyPrint-Override">builder.Services.AddScoped();
< /code>
und die Klasse hat < /p>
private readonly IConfiguration _configuration;
public PDFGenerator(IConfiguration configuration)
{
_configuration = configuration;
}
< /code>
Das Problem, mit dem ich ausgesetzt bin, ist, wenn ich versuche, auf Werte aus der Konfiguration in der PDFGenerator -Klasse zugreifen zu können, erhalte ich immer noch Werte von Appstings.json anstelle von Appsating.development.json. Versucht < /p>
builder.Services.Configure(config);
Was fehlt mir?