Skip to main content

ServiceCollectionExtensions

Namespace: RA.Utilities.Integrations.Extensions

The ServiceCollectionExtensions class is the primary entry point for setting up HttpClient integrations within the .NET dependency injection container. Its methods are declared as C# 14 extension members on IServiceCollection (inside an extension(IServiceCollection) block), so they are called with the familiar services.Method(...) syntax.

AddHttpClientIntegration<...>()​

This is the main method for registering a typed HttpClient. It binds configuration from appsettings.json, validates it on startup, and configures the client's BaseAddress and Timeout.

Parameters​

ParameterTypeDescription
servicesIServiceCollectionThe IServiceCollection to add the services to.
configurationSectionIConfigurationSectionThe configuration section (e.g., from appsettings.json) to bind the settings from.
TInterfaceclassThe interface of the typed HttpClient to register.
TClientclass, TInterfaceThe concrete implementation of the typed HttpClient.
TSettingsclass, [IIntegrationSettings](../Abstractions/IIntegrationSettings.md)The settings class that holds the configuration for this integration.
ReturnsIHttpClientBuilderAn IHttpClientBuilder that can be used to further configure the client pipeline — see HttpClientBuilderExtensions.

Example​

This example shows how to register a WeatherApiClient with its settings.

  1. appsettings.json configuration:
{
"WeatherApi": {
"BaseUrl": "https://api.weather.com/v1/",
"Timeout": 30,
"Actions": {
"GetCurrent": "current"
}
}
}
  1. Define Settings and Client classes:
// Settings classes
public class WeatherApiSettings : BaseApiSettings<WeatherApiActions> { }
public class WeatherApiActions
{
public required string GetCurrent { get; set; }
}

// Client interface and implementation
public interface IWeatherApiClient
{
Task<string> GetCurrentWeatherAsync(string city);
}

public class WeatherApiClient : IWeatherApiClient
{
private readonly HttpClient _httpClient;
private readonly WeatherApiSettings _settings;

public WeatherApiClient(HttpClient httpClient, IOptions<WeatherApiSettings> settings)
{
_httpClient = httpClient;
_settings = settings.Value;
}

public async Task<string> GetCurrentWeatherAsync(string city)
{
var endpoint = $"{_settings.Actions.GetCurrent}?city={city}";
return await _httpClient.GetStringAsync(endpoint);
}
}
  1. Register in Program.cs:
// In Program.cs
using RA.Utilities.Integrations.Extensions;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddHttpClientIntegration<IWeatherApiClient, WeatherApiClient, WeatherApiSettings>(
builder.Configuration.GetSection("WeatherApi")
);

AddScopedHttpMessageHandler<T>()​

Registers a DelegatingHandler with a scoped lifetime in the DI container.

Parameters​

ParameterTypeDescription
servicesIServiceCollectionThe IServiceCollection to add the services to.
TDelegatingHandlerThe handler type to register.
ReturnsIServiceCollectionThe original IServiceCollection for chaining.

Example​

// In Program.cs
builder.Services.AddScopedHttpMessageHandler<MyCustomHandler>();

AddTransientHttpMessageHandler<T>()​

Registers a DelegatingHandler with a transient lifetime in the DI container. This is the most common lifetime for handlers.

Parameters​

ParameterTypeDescription
servicesIServiceCollectionThe IServiceCollection to add the services to.
TDelegatingHandlerThe handler type to register.
ReturnsIServiceCollectionThe original IServiceCollection for chaining.

Example​

// In Program.cs
builder.Services.AddTransientHttpMessageHandler<MyCustomHandler>();
v10.2.0 class rename

These members were previously declared in a single DependencyInjectionExtensions class. In v10.2.0 that class was split into ServiceCollectionExtensions (the methods above) and HttpClientBuilderExtensions (the pipeline methods), and the members were refactored to C# 14 extension members. Method names, signatures, and the RA.Utilities.Integrations.Extensions namespace are unchanged — see the migration guide for details.