Added `services.AddScoped<ReCClient>()` to both `AddRecClient` method overloads in `DependencyInjection.cs` to ensure proper scoped registration of `ReCClient`. Updated `ReC.Client.csproj` to include necessary package references for dependency injection and HTTP client support: - `Microsoft.Extensions.DependencyInjection` (v10.0.0) - `Microsoft.Extensions.Http` (v8.0.0) - `System.Net.Http` (v4.3.4)
41 lines
1.9 KiB
C#
41 lines
1.9 KiB
C#
using Microsoft.Extensions.DependencyInjection;
|
|
#if NETFRAMEWORK
|
|
using System;
|
|
using System.Net.Http;
|
|
#endif
|
|
|
|
namespace ReC.Client
|
|
{
|
|
/// <summary>
|
|
/// Provides extension methods for setting up the ReC client in an <see cref="IServiceCollection"/>.
|
|
/// </summary>
|
|
public static class DependencyInjection
|
|
{
|
|
/// <summary>
|
|
/// Adds and configures the <see cref="HttpClient"/> for the <see cref="ReCClient"/> to the specified <see cref="IServiceCollection"/>
|
|
/// </summary>
|
|
/// <param name="services">The <see cref="IServiceCollection"/> to add the services to.</param>
|
|
/// <param name="apiUri">The base URI of the ReC API.</param>
|
|
/// <returns>An <see cref="IHttpClientBuilder"/> that can be used to configure the client.</returns>
|
|
public static IHttpClientBuilder AddRecClient(this IServiceCollection services, string apiUri)
|
|
{
|
|
services.AddScoped<ReCClient>();
|
|
return services.AddHttpClient(ReCClient.ClientName, client =>
|
|
{
|
|
client.BaseAddress = new Uri(apiUri);
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds and configures the <see cref="HttpClient"/> for the <see cref="ReCClient"/> to the specified <see cref="IServiceCollection"/>
|
|
/// </summary>
|
|
/// <param name="services">The <see cref="IServiceCollection"/> to add the services to.</param>
|
|
/// <param name="configureClient">An action to configure the <see cref="HttpClient"/>.</param>
|
|
/// <returns>An <see cref="IHttpClientBuilder"/> that can be used to configure the client.</returns>
|
|
public static IHttpClientBuilder AddRecClient(this IServiceCollection services, Action<HttpClient> configureClient)
|
|
{
|
|
services.AddScoped<ReCClient>();
|
|
return services.AddHttpClient(ReCClient.ClientName, configureClient);
|
|
}
|
|
}
|
|
} |