38 lines
1.5 KiB
C#
38 lines
1.5 KiB
C#
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, string path = "")
|
|
{
|
|
services.TryAddSingleton<HttpClient>();
|
|
services.TryAddSingleton<CookieContainer>();
|
|
services.AddSingleton<IBaseHttpClientService, BaseHttpClientService>();
|
|
services.Configure<HttpClientOptions>(opt =>
|
|
{
|
|
opt.Uri = uri;
|
|
opt.Path = path;
|
|
});
|
|
|
|
return services;
|
|
}
|
|
|
|
public static IServiceCollection AddHttpClientService<TClientOptions>(this IServiceCollection services, Action<TClientOptions>? clientOptions = null, bool setAsDefaultBase = false)
|
|
where TClientOptions : HttpClientOptions
|
|
{
|
|
services.TryAddSingleton<HttpClient>();
|
|
services.TryAddSingleton<CookieContainer>();
|
|
services.TryAddSingleton<IHttpClientService<TClientOptions>, HttpClientService<TClientOptions>>();
|
|
services.Configure(clientOptions ?? (_ => { }));
|
|
|
|
if (setAsDefaultBase)
|
|
services.TryAddSingleton<IBaseHttpClientService, HttpClientService<TClientOptions>>();
|
|
|
|
return services;
|
|
}
|
|
}
|
|
} |