From cb28ce39a1b541bbe105dde18237bf7357ece415 Mon Sep 17 00:00:00 2001 From: Developer 02 Date: Tue, 11 Jun 2024 18:57:44 +0200 Subject: [PATCH] =?UTF-8?q?Hinzuf=C3=BCgen=20der=20ConfigureBySection-Meth?= =?UTF-8?q?ode=20zur=20Konfiguration=20von=20Diensten=20nach=20Abschnitt?= =?UTF-8?q?=20und=20R=C3=BCckgabe=20des=20Builders.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DigitalData.Core.API/DIExtensions.cs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/DigitalData.Core.API/DIExtensions.cs b/DigitalData.Core.API/DIExtensions.cs index 3a6fa68..a1145b1 100644 --- a/DigitalData.Core.API/DIExtensions.cs +++ b/DigitalData.Core.API/DIExtensions.cs @@ -1,4 +1,5 @@ using Microsoft.AspNetCore.Builder; +using System.Configuration; namespace DigitalData.Core.API { @@ -47,5 +48,22 @@ namespace DigitalData.Core.API /// The WebApplication instance. /// True if the environment is Development or DiP mode is enabled; otherwise, false. public static bool IsDevOrDiP(this WebApplication app) => app.Environment.IsDevelopment() || app.IsDiP(); - } + + /// + /// Configures the services with options from the specified section of the appsettings.json file. + /// + /// The options class type. Must be a reference type. + /// The WebApplicationBuilder instance. + /// The WebApplicationBuilder instance for chaining. + /// Thrown if the section is not found in the configuration. + public static WebApplicationBuilder ConfigureBySection(this WebApplicationBuilder builder) where T : class + { + var section = builder.Configuration.GetSection(typeof(T).Name); + if (!section.Exists()) + throw new InvalidOperationException($"Section '{typeof(T).Name}' not found in appsettings."); + + builder.Services.Configure(section); + return builder; + } + } } \ No newline at end of file