Compare commits

...

2 Commits

Author SHA1 Message Date
Developer 02
246184165f feat(Core.Abstractions): Upgrade auf 3.6 2025-04-28 16:11:26 +02:00
Developer 02
9d36ced82f feat: Hinzufügen von Erweiterungsmethoden zum Abrufen von Konfigurationsoptionen von IServiceProvider
- Implementiert GetOptions<TOptions> zum Abrufen von Optionen oder null.
- Implementiert GetRequiredOptions<TOptions> für den Abruf von Optionen oder das Auslösen einer Ausnahme.
2025-04-28 16:10:31 +02:00
2 changed files with 34 additions and 3 deletions

View File

@ -17,9 +17,9 @@
<RepositoryUrl>http://git.dd:3000/AppStd/WebCoreModules.git</RepositoryUrl> <RepositoryUrl>http://git.dd:3000/AppStd/WebCoreModules.git</RepositoryUrl>
<PackAsTool>False</PackAsTool> <PackAsTool>False</PackAsTool>
<PackageIcon>core_icon.png</PackageIcon> <PackageIcon>core_icon.png</PackageIcon>
<Version>3.5.0</Version> <Version>3.6.0</Version>
<AssemblyVersion>3.5.0</AssemblyVersion> <AssemblyVersion>3.6.0</AssemblyVersion>
<FileVersion>3.5.0</FileVersion> <FileVersion>3.6.0</FileVersion>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@ -0,0 +1,31 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
namespace DigitalData.Core.Abstractions;
/// <summary>
/// Extension methods for <see cref="IServiceProvider"/> to retrieve configuration options.
/// </summary>
public static class ServiceProviderExtensions
{
/// <summary>
/// Retrieves an instance of <typeparamref name="TOptions"/> from the <see cref="IServiceProvider"/>.
/// If the options are not registered, returns null.
/// </summary>
/// <typeparam name="TOptions">The type of the options to retrieve.</typeparam>
/// <param name="service">The service provider instance.</param>
/// <returns>An instance of <typeparamref name="TOptions"/> or null if not found.</returns>
public static TOptions? GetOptions<TOptions>(this IServiceProvider service) where TOptions : class
=> service.GetService<IOptions<TOptions>>()?.Value;
/// <summary>
/// Retrieves an instance of <typeparamref name="TOptions"/> from the <see cref="IServiceProvider"/>.
/// Throws an exception if the options are not registered.
/// </summary>
/// <typeparam name="TOptions">The type of the options to retrieve.</typeparam>
/// <param name="service">The service provider instance.</param>
/// <returns>An instance of <typeparamref name="TOptions"/>.</returns>
/// <exception cref="InvalidOperationException">Thrown when the options are not registered.</exception>
public static TOptions GetRequiredOptions<TOptions>(this IServiceProvider service) where TOptions : class
=> service.GetRequiredService<IOptions<TOptions>>().Value;
}