refactor(DIExtensions): Alle TryAddSingelton wurden in AddSingelton umgewandelt, um im Falle einer falschen Ausnahme den Fehler zu protokollieren.

- SetAsDefault-Parameter hinzugefügt, um nicht-generische IRSAFactory im Falle einer Konfiguration über appsettings registrieren zu können.
This commit is contained in:
Developer 02 2024-12-13 14:00:43 +01:00
parent 7459f05748
commit 82aa8d1143

View File

@ -3,7 +3,6 @@ using DigitalData.Core.Security.Config;
using DigitalData.Core.Security.Cryptographer; using DigitalData.Core.Security.Cryptographer;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using System.Text.Json; using System.Text.Json;
using System.Text.Json.Serialization; using System.Text.Json.Serialization;
@ -22,31 +21,23 @@ namespace DigitalData.Core.Security
return options; return options;
} }
private static IServiceCollection AddAsymCryptService<TAsymCryptParams>(this IServiceCollection services) private static IServiceCollection AddAsymCryptService<TAsymCryptParams>(this IServiceCollection services) where TAsymCryptParams : AsymCryptParams
where TAsymCryptParams : AsymCryptParams => services.AddScoped<IAsymCryptService<TAsymCryptParams>, AsymCryptService<TAsymCryptParams>>();
{
services.TryAddScoped<IAsymCryptService<TAsymCryptParams>, AsymCryptService<TAsymCryptParams>>();
return services;
}
public static IServiceCollection AddAsymCryptService<TAsymCryptParams>(this IServiceCollection services, IConfigurationSection section) public static IServiceCollection AddAsymCryptService<TAsymCryptParams>(this IServiceCollection services, IConfigurationSection section)
where TAsymCryptParams : AsymCryptParams where TAsymCryptParams : AsymCryptParams
=> services.Configure<TAsymCryptParams>(section).AddAsymCryptService<TAsymCryptParams>(); => services.Configure<TAsymCryptParams>(section).AddAsymCryptService<TAsymCryptParams>();
public static IServiceCollection AddAsymCryptService<TAsymCryptParams>(this IServiceCollection services, TAsymCryptParams param) public static IServiceCollection AddAsymCryptService<TAsymCryptParams>(this IServiceCollection services, TAsymCryptParams param)
where TAsymCryptParams : AsymCryptParams where TAsymCryptParams : AsymCryptParams
=> services.AddSingleton(Options.Create(param)).AddAsymCryptService<TAsymCryptParams>(); => services.AddSingleton(Options.Create(param)).AddAsymCryptService<TAsymCryptParams>();
/// <summary> /// <summary>
/// Registers default RSA Factory instance with default params /// Registers default RSA Factory instance with default params
/// </summary> /// </summary>
/// <param name="services"></param> /// <param name="services"></param>
/// <returns></returns> /// <returns>The updated <see cref="IServiceCollection"/> with the RSA Factory registered.</returns>
public static IServiceCollection AddRSAFactory(this IServiceCollection services) public static IServiceCollection AddRSAFactory(this IServiceCollection services) => services.AddScoped(_ => Instance.RSAFactory);
{
services.TryAddScoped(_ => Instance.RSAFactory);
return services;
}
/// <summary> /// <summary>
/// Registers a custom RSA Factory instance with specified parameters from the given configuration section. /// Registers a custom RSA Factory instance with specified parameters from the given configuration section.
@ -54,13 +45,15 @@ namespace DigitalData.Core.Security
/// <typeparam name="TRSAFactoryParams"></typeparam> /// <typeparam name="TRSAFactoryParams"></typeparam>
/// <param name="services"></param> /// <param name="services"></param>
/// <param name="section"></param> /// <param name="section"></param>
/// <returns></returns> /// <param name="setAsDefault">If true, the factory is registered as the default <see cref="IRSAFactory"/>. Otherwise, it is registered as <see cref="IRSAFactory{TRSAFactoryParams}"/>.</param>
public static IServiceCollection AddRSAFactory<TRSAFactoryParams>(this IServiceCollection services, IConfigurationSection section) /// <returns>The updated <see cref="IServiceCollection"/> with the RSA Factory registered.</returns>
public static IServiceCollection AddRSAFactory<TRSAFactoryParams>(this IServiceCollection services, IConfigurationSection section, bool setAsDefault = false)
where TRSAFactoryParams : RSAFactoryParams where TRSAFactoryParams : RSAFactoryParams
{ {
services.Configure<TRSAFactoryParams>(section); services.Configure<TRSAFactoryParams>(section);
services.TryAddSingleton<IRSAFactory<TRSAFactoryParams>, RSAFactory<TRSAFactoryParams>>(); return setAsDefault
return services; ? services.AddSingleton<IRSAFactory, RSAFactory<TRSAFactoryParams>>()
: services.AddSingleton<IRSAFactory<TRSAFactoryParams>, RSAFactory<TRSAFactoryParams>>();
} }
/// <summary> /// <summary>
@ -74,12 +67,10 @@ namespace DigitalData.Core.Security
public static IServiceCollection AddRSAFactory<TRSAFactoryParams>(this IServiceCollection services, TRSAFactoryParams rsaParams, bool setAsDefault = false) public static IServiceCollection AddRSAFactory<TRSAFactoryParams>(this IServiceCollection services, TRSAFactoryParams rsaParams, bool setAsDefault = false)
where TRSAFactoryParams : RSAFactoryParams where TRSAFactoryParams : RSAFactoryParams
{ {
services.TryAddSingleton(Options.Create(rsaParams)); services.AddSingleton(Options.Create(rsaParams));
if (setAsDefault) return setAsDefault
services.TryAddSingleton<IRSAFactory, RSAFactory<TRSAFactoryParams>>(); ? services.AddSingleton<IRSAFactory, RSAFactory<TRSAFactoryParams>>()
else : services.AddSingleton<IRSAFactory<TRSAFactoryParams>, RSAFactory<TRSAFactoryParams>>();
services.TryAddSingleton<IRSAFactory<TRSAFactoryParams>, RSAFactory<TRSAFactoryParams>>();
return services;
} }
} }
} }