feat: DI-Erweiterung für HttpClient-Services verbessert
- `AddHttpClientService` refaktoriert, um `AddHttpClientServiceDefaults` für gemeinsame Setup-Logik einzuführen. - Überladungen für `AddHttpClientService` hinzugefügt, um sowohl `IConfigurationSection` als auch direkte Options-Instanz zu unterstützen. - Bessere Erweiterbarkeit und sauberere Service-Konfiguration sichergestellt.
This commit is contained in:
parent
ef7da0e52c
commit
f9df2fb29e
@ -2,21 +2,35 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using Microsoft.Extensions.Options;
|
||||
using System.Net;
|
||||
|
||||
namespace DigitalData.Core.Client
|
||||
{
|
||||
public static class DIExtensions
|
||||
{
|
||||
public static IServiceCollection AddHttpClientService<THttpClientOptions>(this IServiceCollection services, IConfigurationSection? section = null)
|
||||
where THttpClientOptions : class, IHttpClientOptions
|
||||
internal static IServiceCollection AddHttpClientServiceDefaults(this IServiceCollection services)
|
||||
{
|
||||
services.TryAddSingleton<HttpClient>();
|
||||
services.TryAddSingleton<CookieContainer>();
|
||||
services.TryAddSingleton<IBaseHttpClientService, BaseHttpClientService>();
|
||||
if(section is not null)
|
||||
services.Configure<THttpClientOptions>(section);
|
||||
return services;
|
||||
}
|
||||
|
||||
public static IServiceCollection AddHttpClientService<THttpClientOptions>(this IServiceCollection services, IConfigurationSection section)
|
||||
where THttpClientOptions : class, IHttpClientOptions
|
||||
{
|
||||
services.AddHttpClientServiceDefaults();
|
||||
services.TryAddSingleton<IHttpClientService<THttpClientOptions>, HttpClientService<THttpClientOptions>>();
|
||||
return services.Configure<THttpClientOptions>(section);
|
||||
}
|
||||
|
||||
public static IServiceCollection AddHttpClientService<THttpClientOptions>(this IServiceCollection services, THttpClientOptions options)
|
||||
where THttpClientOptions : class, IHttpClientOptions
|
||||
{
|
||||
services.AddHttpClientServiceDefaults();
|
||||
services.TryAddSingleton<IHttpClientService<THttpClientOptions>, HttpClientService<THttpClientOptions>>();
|
||||
services.TryAddSingleton(Options.Create(options));
|
||||
return services;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user