feat: Implementierung von BaseHttpClientService und DIExtensions für HTTP-Client-Dienste
- Hinzugefügt: `BaseHttpClientService` zur Handhabung von HTTP-Anfragen mit Cookie-Verwaltung. - Implementiert: `HttpClientService<TClientOptions>`, das `BaseHttpClientService` für typisierte Client-Optionen erweitert. - Erstellt: `DIExtensions` zur Registrierung von HTTP-Client-Diensten im Dependency Injection Container. - Bereitgestellt: Methoden zum Hinzufügen von HTTP-Client-Diensten mit und ohne spezifische Client-Optionen. - Konfiguriert: Optionen zum Festlegen der Basis-URI für HTTP-Clients.
This commit is contained in:
34
DigitalData.Core.Client/DIExtensions.cs
Normal file
34
DigitalData.Core.Client/DIExtensions.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using DigitalData.Core.Abstractions.Client;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using System.Net;
|
||||
|
||||
namespace DigitalData.Core.Client
|
||||
{
|
||||
public static class DIExtensions
|
||||
{
|
||||
public static IServiceCollection AddHttpClientService(this IServiceCollection services, string uri)
|
||||
{
|
||||
services.TryAddSingleton<HttpClient>();
|
||||
services.TryAddSingleton<CookieContainer>();
|
||||
services.AddSingleton<IBaseHttpClientService, BaseHttpClientService>();
|
||||
services.Configure<HttpClientOptions>(opt => opt.Uri = uri);
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
public static IServiceCollection AddHttpClientService<TClientOptions>(this IServiceCollection services, Action<TClientOptions>? clientOptions = null, bool setAsDefault = false)
|
||||
where TClientOptions : HttpClientOptions, new()
|
||||
{
|
||||
services.TryAddSingleton<HttpClient>();
|
||||
services.TryAddSingleton<CookieContainer>();
|
||||
services.AddSingleton<IHttpClientService<TClientOptions>, HttpClientService<TClientOptions>>();
|
||||
services.Configure(clientOptions ?? (_ => { }));
|
||||
|
||||
if (setAsDefault)
|
||||
services.AddSingleton<IBaseHttpClientService, HttpClientService<TClientOptions>>();
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user