Add optional ReCClientOptions configuration support
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.
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
#if NETFRAMEWORK
|
|
||||||
using System;
|
using System;
|
||||||
|
#if NETFRAMEWORK
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -16,9 +16,15 @@ namespace ReC.Client
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="services">The <see cref="IServiceCollection"/> to add the services to.</param>
|
/// <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="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>
|
/// <returns>An <see cref="IHttpClientBuilder"/> that can be used to configure the client.</returns>
|
||||||
public static IHttpClientBuilder AddRecClient(this IServiceCollection services, string apiUri)
|
#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>();
|
services.AddScoped<ReCClient>();
|
||||||
return services.AddHttpClient(ReCClient.ClientName, client =>
|
return services.AddHttpClient(ReCClient.ClientName, client =>
|
||||||
{
|
{
|
||||||
@@ -31,11 +37,29 @@ namespace ReC.Client
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="services">The <see cref="IServiceCollection"/> to add the services to.</param>
|
/// <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="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>
|
/// <returns>An <see cref="IHttpClientBuilder"/> that can be used to configure the client.</returns>
|
||||||
public static IHttpClientBuilder AddRecClient(this IServiceCollection services, Action<HttpClient> configureClient)
|
#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>();
|
services.AddScoped<ReCClient>();
|
||||||
return services.AddHttpClient(ReCClient.ClientName, configureClient);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user