Added an optional `configureOptions` parameter to `AddRecClient` methods, enabling configuration of `ReCClientOptions`. Introduced conditional compilation to handle nullability differences between .NET Framework and other frameworks. Implemented a private helper method `AddRecClientOptions` to ensure default options are registered even when no configuration action is provided. Updated `AddRecClient` overloads to use this helper. Included `System.Net.Http` in `#if NETFRAMEWORK` directives to maintain compatibility with .NET Framework.
65 lines
3.3 KiB
C#
65 lines
3.3 KiB
C#
using Microsoft.Extensions.DependencyInjection;
|
|
using System;
|
|
#if NETFRAMEWORK
|
|
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>
|
|
/// <param name="configureOptions">An optional action to configure <see cref="ReCClientOptions"/>. When omitted, defaults are used.</param>
|
|
/// <returns>An <see cref="IHttpClientBuilder"/> that can be used to configure the client.</returns>
|
|
#if NETFRAMEWORK
|
|
public static IHttpClientBuilder AddRecClient(this IServiceCollection services, string apiUri, Action<ReCClientOptions> configureOptions = null)
|
|
#else
|
|
public static IHttpClientBuilder AddRecClient(this IServiceCollection services, string apiUri, Action<ReCClientOptions>? configureOptions = null)
|
|
#endif
|
|
{
|
|
AddRecClientOptions(services, configureOptions);
|
|
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>
|
|
/// <param name="configureOptions">An optional action to configure <see cref="ReCClientOptions"/>. When omitted, defaults are used.</param>
|
|
/// <returns>An <see cref="IHttpClientBuilder"/> that can be used to configure the client.</returns>
|
|
#if NETFRAMEWORK
|
|
public static IHttpClientBuilder AddRecClient(this IServiceCollection services, Action<HttpClient> configureClient, Action<ReCClientOptions> configureOptions = null)
|
|
#else
|
|
public static IHttpClientBuilder AddRecClient(this IServiceCollection services, Action<HttpClient> configureClient, Action<ReCClientOptions>? configureOptions = null)
|
|
#endif
|
|
{
|
|
AddRecClientOptions(services, configureOptions);
|
|
services.AddScoped<ReCClient>();
|
|
return services.AddHttpClient(ReCClient.ClientName, configureClient);
|
|
}
|
|
|
|
#if NETFRAMEWORK
|
|
private static void AddRecClientOptions(IServiceCollection services, Action<ReCClientOptions> configureOptions)
|
|
#else
|
|
private static void AddRecClientOptions(IServiceCollection services, Action<ReCClientOptions>? configureOptions)
|
|
#endif
|
|
{
|
|
// Ensure default options are always registered even when the caller does not configure anything.
|
|
var builder = services.AddOptions<ReCClientOptions>();
|
|
if (configureOptions != null)
|
|
builder.Configure(configureOptions);
|
|
}
|
|
}
|
|
} |