feat(infrastructure): add extension methods for configuring and registering HttpClient
- Implemented ConfigureHttpClient extension for IServiceCollection with dynamic client name - Added AddInfrastructureServices method to simplify HttpClient registration via configurable options - Introduced sealed Config class for flexible client configuration
This commit is contained in:
34
src/Leanetec.EConnect.Infrastructure/DependencyInjection.cs
Normal file
34
src/Leanetec.EConnect.Infrastructure/DependencyInjection.cs
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
|
namespace Leanetec.EConnect.Infrastructure;
|
||||||
|
|
||||||
|
public static class DependencyInjection
|
||||||
|
{
|
||||||
|
internal static readonly string HttpClientName = Guid.NewGuid().ToString();
|
||||||
|
|
||||||
|
internal static IServiceCollection ConfigureHttpClient(this IServiceCollection services, Action<HttpClient> configureClient)
|
||||||
|
{
|
||||||
|
services.AddHttpClient(HttpClientName, configureClient);
|
||||||
|
return services;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IServiceCollection AddInfrastructureServices(this IServiceCollection services, Action<Config> options)
|
||||||
|
{
|
||||||
|
Config config = new(services);
|
||||||
|
options.Invoke(config);
|
||||||
|
services.ConfigureHttpClient(config.Client);
|
||||||
|
return services;
|
||||||
|
}
|
||||||
|
|
||||||
|
public sealed class Config
|
||||||
|
{
|
||||||
|
private readonly IServiceCollection _services;
|
||||||
|
|
||||||
|
internal Config(IServiceCollection services)
|
||||||
|
{
|
||||||
|
_services = services;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Action<HttpClient> Client { get; set; } = _ => { };
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,4 +6,9 @@
|
|||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.2" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.1" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
Reference in New Issue
Block a user