File: DependencyInjection\HeaderPropagationServiceCollectionExtensions.cs
Web Access
Project: src\src\Middleware\HeaderPropagation\src\Microsoft.AspNetCore.HeaderPropagation.csproj (Microsoft.AspNetCore.HeaderPropagation)
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
 
using System.Net.Http;
using Microsoft.AspNetCore.HeaderPropagation;
using Microsoft.Extensions.DependencyInjection.Extensions;
 
namespace Microsoft.Extensions.DependencyInjection;
 
/// <summary>
/// <see cref="IServiceCollection"/> extension methods for <see cref="HeaderPropagationMiddleware"/> which propagates request headers to an <see cref="System.Net.Http.HttpClient"/>.
/// </summary>
public static class HeaderPropagationServiceCollectionExtensions
{
    /// <summary>
    /// Adds services required for propagating headers to a <see cref="HttpClient"/>.
    /// </summary>
    /// <param name="services">The <see cref="IServiceCollection"/> to add the services to.</param>
    /// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
    public static IServiceCollection AddHeaderPropagation(this IServiceCollection services)
    {
        ArgumentNullException.ThrowIfNull(services);
 
        services.TryAddSingleton<HeaderPropagationValues>();
 
        return services;
    }
 
    /// <summary>
    /// Adds services required for propagating headers to a <see cref="HttpClient"/>.
    /// </summary>
    /// <param name="services">The <see cref="IServiceCollection"/> to add the services to.</param>
    /// <param name="configureOptions">A delegate used to configure the <see cref="HeaderPropagationOptions"/>.</param>
    /// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
    public static IServiceCollection AddHeaderPropagation(this IServiceCollection services, Action<HeaderPropagationOptions> configureOptions)
    {
        ArgumentNullException.ThrowIfNull(services);
        ArgumentNullException.ThrowIfNull(configureOptions);
 
        services.Configure(configureOptions);
        services.AddHeaderPropagation();
 
        return services;
    }
}