Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HttpClientFactory Header Propagation MessageHandler #1099

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 30 additions & 0 deletions Extensions.sln
Expand Up @@ -297,6 +297,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tools", "tools", "{B0D89499
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "testassets", "testassets", "{15AC3300-D335-4C5C-9E3A-22F26904AB26}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HeaderPropagation.Tests", "src\HttpClientFactory\HeaderPropagation\test\HeaderPropagation.Tests\HeaderPropagation.Tests.csproj", "{71778E1A-E77E-42F6-9346-A49A1727E921}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Extensions.Http.HeaderPropagation", "src\HttpClientFactory\HeaderPropagation\src\Microsoft.Extensions.Http.HeaderPropagation.csproj", "{C948927D-0B78-478D-8888-C437BEA148E6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -1771,6 +1775,30 @@ Global
{DE47CB52-3E6F-4A6A-910E-AE118A00CE1A}.Release|x64.Build.0 = Release|Any CPU
{DE47CB52-3E6F-4A6A-910E-AE118A00CE1A}.Release|x86.ActiveCfg = Release|Any CPU
{DE47CB52-3E6F-4A6A-910E-AE118A00CE1A}.Release|x86.Build.0 = Release|Any CPU
{71778E1A-E77E-42F6-9346-A49A1727E921}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{71778E1A-E77E-42F6-9346-A49A1727E921}.Debug|Any CPU.Build.0 = Debug|Any CPU
{71778E1A-E77E-42F6-9346-A49A1727E921}.Debug|x64.ActiveCfg = Debug|Any CPU
{71778E1A-E77E-42F6-9346-A49A1727E921}.Debug|x64.Build.0 = Debug|Any CPU
{71778E1A-E77E-42F6-9346-A49A1727E921}.Debug|x86.ActiveCfg = Debug|Any CPU
{71778E1A-E77E-42F6-9346-A49A1727E921}.Debug|x86.Build.0 = Debug|Any CPU
{71778E1A-E77E-42F6-9346-A49A1727E921}.Release|Any CPU.ActiveCfg = Release|Any CPU
{71778E1A-E77E-42F6-9346-A49A1727E921}.Release|Any CPU.Build.0 = Release|Any CPU
{71778E1A-E77E-42F6-9346-A49A1727E921}.Release|x64.ActiveCfg = Release|Any CPU
{71778E1A-E77E-42F6-9346-A49A1727E921}.Release|x64.Build.0 = Release|Any CPU
{71778E1A-E77E-42F6-9346-A49A1727E921}.Release|x86.ActiveCfg = Release|Any CPU
{71778E1A-E77E-42F6-9346-A49A1727E921}.Release|x86.Build.0 = Release|Any CPU
{C948927D-0B78-478D-8888-C437BEA148E6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C948927D-0B78-478D-8888-C437BEA148E6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C948927D-0B78-478D-8888-C437BEA148E6}.Debug|x64.ActiveCfg = Debug|Any CPU
{C948927D-0B78-478D-8888-C437BEA148E6}.Debug|x64.Build.0 = Debug|Any CPU
{C948927D-0B78-478D-8888-C437BEA148E6}.Debug|x86.ActiveCfg = Debug|Any CPU
{C948927D-0B78-478D-8888-C437BEA148E6}.Debug|x86.Build.0 = Debug|Any CPU
{C948927D-0B78-478D-8888-C437BEA148E6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C948927D-0B78-478D-8888-C437BEA148E6}.Release|Any CPU.Build.0 = Release|Any CPU
{C948927D-0B78-478D-8888-C437BEA148E6}.Release|x64.ActiveCfg = Release|Any CPU
{C948927D-0B78-478D-8888-C437BEA148E6}.Release|x64.Build.0 = Release|Any CPU
{C948927D-0B78-478D-8888-C437BEA148E6}.Release|x86.ActiveCfg = Release|Any CPU
{C948927D-0B78-478D-8888-C437BEA148E6}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -1904,6 +1932,8 @@ Global
{89B01C5B-42DF-4E99-847E-354B8C5FBA6F} = {6A293FDC-A13B-4137-87F9-C9225CF3542B}
{DE47CB52-3E6F-4A6A-910E-AE118A00CE1A} = {B0D89499-D1FA-4113-88C7-B82AB1D98EB6}
{15AC3300-D335-4C5C-9E3A-22F26904AB26} = {36617B81-CF74-4FCB-A7CA-E95DF3CA92FC}
{71778E1A-E77E-42F6-9346-A49A1727E921} = {2930377F-5CDC-4826-A885-B5AE5CAF9EB3}
{C948927D-0B78-478D-8888-C437BEA148E6} = {2930377F-5CDC-4826-A885-B5AE5CAF9EB3}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {814BFC88-0867-451F-AC8F-20FE107809B4}
Expand Down
11 changes: 11 additions & 0 deletions eng/Baseline.Designer.props
Expand Up @@ -4,6 +4,17 @@
<MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
<ExtensionsBaselineVersion>2.2.1</ExtensionsBaselineVersion>
</PropertyGroup>
<!-- Package: Microsoft.AspNetCore.Http-->
<PropertyGroup Condition=" '$(PackageId)' == 'Microsoft.AspNetCore.Http' ">
<BaselinePackageVersion>2.2.0</BaselinePackageVersion>
</PropertyGroup>
<ItemGroup Condition=" '$(PackageId)' == 'Microsoft.AspNetCore.Http' AND '$(TargetFramework)' == 'netstandard2.0' ">
<BaselinePackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="[2.2.0, )" />
<BaselinePackageReference Include="Microsoft.AspNetCore.WebUtilities" Version="[2.2.0, )" />
<BaselinePackageReference Include="Microsoft.Net.Http.Headers" Version="[2.2.0, )" />
<BaselinePackageReference Include="Microsoft.Extensions.ObjectPool" Version="[2.2.0, )" />
<BaselinePackageReference Include="Microsoft.Extensions.Options" Version="[2.2.0, )" />
</ItemGroup>
<!-- Package: Microsoft.Extensions.Caching.Abstractions-->
<PropertyGroup Condition=" '$(PackageId)' == 'Microsoft.Extensions.Caching.Abstractions' ">
<BaselinePackageVersion>2.2.0</BaselinePackageVersion>
Expand Down
1 change: 1 addition & 0 deletions eng/Baseline.xml
Expand Up @@ -5,6 +5,7 @@ build of Extensions. Update this list when preparing for a new patch.

-->
<Baseline Version="2.2.1">
<Package Id="Microsoft.AspNetCore.Http" Version="2.2.0" />
<Package Id="Microsoft.Extensions.Caching.Abstractions" Version="2.2.0" />
<Package Id="Microsoft.Extensions.Caching.Memory" Version="2.2.0" />
<Package Id="Microsoft.Extensions.Caching.Redis" Version="2.2.0" />
Expand Down
1 change: 1 addition & 0 deletions eng/Dependencies.props
Expand Up @@ -52,6 +52,7 @@ and are generated based on the last package release.
<ItemGroup Label="External dependencies">
<LatestPackageReference Include="BenchmarkDotNet" Version="0.10.13" />
<LatestPackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.6" />
<LatestPackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.0" />
<LatestPackageReference Include="Microsoft.Azure.KeyVault" Version="2.3.2" />
<LatestPackageReference Include="Microsoft.Azure.Services.AppAuthentication" Version="1.0.3" />
<LatestPackageReference Include="Microsoft.CodeAnalysis.Common" Version="2.8.0" />
Expand Down
1 change: 1 addition & 0 deletions eng/ProjectReferences.props
Expand Up @@ -35,6 +35,7 @@
<ProjectReferenceProvider Include="Microsoft.Extensions.Diagnostics.HealthChecks" ProjectPath="$(RepoRoot)src\HealthChecks\HealthChecks\src\Microsoft.Extensions.Diagnostics.HealthChecks.csproj" RefProjectPath="$(RepoRoot)src\HealthChecks\HealthChecks\ref\Microsoft.Extensions.Diagnostics.HealthChecks.csproj" />
<ProjectReferenceProvider Include="Microsoft.Extensions.Hosting.Abstractions" ProjectPath="$(RepoRoot)src\Hosting\Abstractions\src\Microsoft.Extensions.Hosting.Abstractions.csproj" RefProjectPath="$(RepoRoot)src\Hosting\Abstractions\ref\Microsoft.Extensions.Hosting.Abstractions.csproj" />
<ProjectReferenceProvider Include="Microsoft.Extensions.Hosting" ProjectPath="$(RepoRoot)src\Hosting\Hosting\src\Microsoft.Extensions.Hosting.csproj" RefProjectPath="$(RepoRoot)src\Hosting\Hosting\ref\Microsoft.Extensions.Hosting.csproj" />
<ProjectReferenceProvider Include="Microsoft.Extensions.Http.HeaderPropagation" ProjectPath="$(RepoRoot)src\HttpClientFactory\HeaderPropagation\src\Microsoft.Extensions.Http.HeaderPropagation.csproj" RefProjectPath="$(RepoRoot)src\HttpClientFactory\HeaderPropagation\ref\Microsoft.Extensions.Http.HeaderPropagation.csproj" />
<ProjectReferenceProvider Include="Microsoft.Extensions.Http" ProjectPath="$(RepoRoot)src\HttpClientFactory\Http\src\Microsoft.Extensions.Http.csproj" RefProjectPath="$(RepoRoot)src\HttpClientFactory\Http\ref\Microsoft.Extensions.Http.csproj" />
<ProjectReferenceProvider Include="Microsoft.Extensions.Http.Polly" ProjectPath="$(RepoRoot)src\HttpClientFactory\Polly\src\Microsoft.Extensions.Http.Polly.csproj" RefProjectPath="$(RepoRoot)src\HttpClientFactory\Polly\ref\Microsoft.Extensions.Http.Polly.csproj" />
<ProjectReferenceProvider Include="Microsoft.JSInterop" ProjectPath="$(RepoRoot)src\JSInterop\Microsoft.JSInterop\src\Microsoft.JSInterop.csproj" RefProjectPath="$(RepoRoot)src\JSInterop\Microsoft.JSInterop\ref\Microsoft.JSInterop.csproj" />
Expand Down
@@ -0,0 +1,35 @@
using System;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Http;
using Microsoft.Extensions.Http.HeaderPropagation;
using Microsoft.Extensions.Options;

namespace Microsoft.Extensions.DependencyInjection
{
public static class HeaderPropagationExtensions
{
public static IServiceCollection AddHeaderPropagation(this IServiceCollection services, Action<HeaderPropagationOptions> configure)
{
services.AddHttpContextAccessor();
services.Configure(configure);
services.TryAddEnumerable(ServiceDescriptor.Singleton<IHttpMessageHandlerBuilderFilter, HeaderPropagationMessageHandlerBuilderFilter>());
return services;
}

public static IHttpClientBuilder AddHeaderPropagation(this IHttpClientBuilder builder, Action<HeaderPropagationOptions> configure)
{
builder.Services.AddHttpContextAccessor();
builder.Services.Configure(configure);
builder.AddHttpMessageHandler((sp) =>
{
var options = sp.GetRequiredService<IOptions<HeaderPropagationOptions>>();
var contextAccessor = sp.GetRequiredService<IHttpContextAccessor>();

return new HeaderPropagationMessageHandler(options.Value, contextAccessor);
});

return builder;
}
}
}
@@ -0,0 +1,16 @@
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Primitives;
using System;
using System.Net.Http;

namespace Microsoft.Extensions.Http.HeaderPropagation
{
public class HeaderPropagationEntry
{
public string InputName { get; set; }
public string OutputName { get; set; }
public StringValues DefaultValues { get; set; }
public Func<HttpRequestMessage, HttpContext, StringValues> DefaultValuesGenerator { get; set; }
public bool AlwaysAdd { get; set; }
}
}
@@ -0,0 +1,54 @@
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Primitives;

namespace Microsoft.Extensions.Http.HeaderPropagation
{
public class HeaderPropagationMessageHandler : DelegatingHandler
{
private readonly HeaderPropagationOptions _options;
private readonly IHttpContextAccessor _contextAccessor;

public HeaderPropagationMessageHandler(HeaderPropagationOptions options, IHttpContextAccessor contextAccessor)
{
_options = options;
_contextAccessor = contextAccessor;
}

protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
if (_contextAccessor.HttpContext != null)
{
foreach (var header in _options.Headers)
{
if (!_contextAccessor.HttpContext.Request.Headers.TryGetValue(header.InputName, out var values)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@davidfowl we've run into trouble trying to use IHttpContextAccessor in DelegatingHandlers before haven't we? People would run their HttpClient requests in parallel and cause unsafe mulithreaded access to HttpContext. When ApplicationInsights dealt with a similar problem they concluded the best way to handle it was to collect fields they needed up front and pass them through in a dedicated data structure.

What if instead of a DelegatingHandler we provided a set of request composition helpers on top of HttpClient and HttpRequestMessage?

The biggest downside I can see would be the caller needing to be HttpContext aware, which may or may not work for various libraries.

Copy link
Author

@alefranz alefranz Feb 15, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Chris,
yes the HttpContext is not thread-safe and it's a really valid point, not sure why I haven't considered sending requests in parallel :(
I should add a HeaderPropagationInitializer similar to the TelemetryInitializer that get the required values from the context and store it in it's own data structure. However this will mean that it needs to be aware of all the headers forwarded by all the HttpClients as we will want to lock on the context only once per MVC request. I'll have to think about it.

About the composition helpers, I'm not sure how that will fit with the HttpClientFactory. Can you elaborate a bit more on what you have in mind?

Btw here is the issue you are referring to:
https://github.com/Microsoft/ApplicationInsights-aspnetcore/issues/373
I remembered he mentioned it on last year talk at NDC :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The composition helpers wouldn't be affiliated with HttpClientFactory, they'd be APIs for directly creating or operating on HttpRequestMessages before calling into HttpClient. They may have extensions on HttpClient to facilitate.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think the right thing to do here is to make a middleware that initializes state, and a standalone DelegatingHandler that reads that state to modify the outgoing request.

I don't think we need to tie this to the http client factory. In general library code doesn't need to integrate with it directly because wiring up a DelegatingHandler is easy.

|| StringValues.IsNullOrEmpty(values))
{
if (header.DefaultValuesGenerator != null)
{
values = header.DefaultValuesGenerator(request, _contextAccessor.HttpContext);
if (StringValues.IsNullOrEmpty(values)) continue;
}
else if (!StringValues.IsNullOrEmpty(header.DefaultValues))
{
values = header.DefaultValues;
}
else
{
continue;
}
}

if (header.AlwaysAdd || !request.Headers.Contains(header.OutputName))
{
request.Headers.TryAddWithoutValidation(header.OutputName, (string[])values);
}
}
}

return base.SendAsync(request, cancellationToken);
}
}
}
@@ -0,0 +1,27 @@
using System;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;

namespace Microsoft.Extensions.Http.HeaderPropagation
{
internal class HeaderPropagationMessageHandlerBuilderFilter : IHttpMessageHandlerBuilderFilter
{
private readonly HeaderPropagationOptions _options;
private readonly IHttpContextAccessor _contextAccessor;

public HeaderPropagationMessageHandlerBuilderFilter(IOptions<HeaderPropagationOptions> options, IHttpContextAccessor contextAccessor)
{
_options = options.Value;
_contextAccessor = contextAccessor;
}

public Action<HttpMessageHandlerBuilder> Configure(Action<HttpMessageHandlerBuilder> next)
{
return builder =>
{
builder.AdditionalHandlers.Add(new HeaderPropagationMessageHandler(_options, _contextAccessor));
next(builder);
};
}
}
}
@@ -0,0 +1,9 @@
using System.Collections.Generic;

namespace Microsoft.Extensions.Http.HeaderPropagation
{
public class HeaderPropagationOptions
{
public IList<HeaderPropagationEntry> Headers { get; set; } = new List<HeaderPropagationEntry>();
}
}
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Description>
The HttpClient factory is a pattern for configuring and retrieving named HttpClients in a composable way. This package add to IHttpClientFactory to propagating headers from the current request to an outgoing http request.
</Description>
<TargetFramework>netstandard2.0</TargetFramework>
<NoWarn>$(NoWarn);CS1591</NoWarn>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageTags>aspnetcore;httpclient</PackageTags>

<RootNamespace>Microsoft.Extensions.Http.HeaderPropagation</RootNamespace>
<IsShipping>true</IsShipping>
</PropertyGroup>

<ItemGroup>
<InternalsVisibleTo Include="Microsoft.Extensions.Http.HeaderPropagation.Tests" />
</ItemGroup>

<ItemGroup>
<Reference Include="Microsoft.AspNetCore.Http" />
<Reference Include="Microsoft.Extensions.Http" />
<Reference Include="Microsoft.Extensions.DependencyInjection" />
</ItemGroup>

</Project>
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp3.0;net472</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
<Reference Include="Microsoft.Extensions.DependencyInjection" />
<Reference Include="Microsoft.Extensions.Http.HeaderPropagation" />
</ItemGroup>

</Project>