HeaderPropagation 2.0.0

ASP.NET Core middleware to propagate HTTP headers from the incoming request to the outgoing HTTP Client requests.
This is a backport to ASP.NET Core 2.1 (and 2.2) of the ASP.NET Core HeaderPropagation middleware I had recently contributed to the ASP.NET Core project.
All code is licensed under the Apache License, Version 2.0 and copyrighted by the .NET Foundation.

There is a newer version of this package available.
See the version list below for details.
Install-Package HeaderPropagation -Version 2.0.0
dotnet add package HeaderPropagation --version 2.0.0
<PackageReference Include="HeaderPropagation" Version="2.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add HeaderPropagation --version 2.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

This is a backport to ASP.NET Core 2.1 (and 2.2) of the HeaderPropagation middleware I had recently contributed to the ASP.NET Core project.
All code is licensed under the Apache License, Version 2.0 and copyrighted by the .NET Foundation.

Motivation

I believe it is a common use case which deserves to be included in ASP.NET Core.
Its main use case is probably to track distributed transaction which requires the ability to pass through a transaction identifier as well as generating a new one when not present.

Given the ASP.NET Core 3.0 release is quite far away, and the current policy doesn't allow to backport new features to already shipped releases, I have created this package as recommended so it can be used today on projects based on ASP.NET Core 2.1 or 2.2.

Usage

In Startup.Configure enable the middleware:

app.UseHeaderPropagation();

In Startup.ConfigureServices add the required services, eventually specifying a configuration action:

services.AddHeaderPropagation(o =>
{
    // propagate the header if present
    o.Headers.Add("User-Agent");

    // if still missing, set it with a value factory
    o.Headers.Add("User-Agent", context => "Mozilla/5.0 (trust me, I'm really Mozilla!)");

    // propagate the header if present, using a different name in the outbound request
    o.Headers.Add("Accept-Language", "Lang");
});

If you are using the HttpClientFactory, add the DelegatingHandler to the client configuration using the AddHeaderPropagation extension method.

services.AddHttpClient<GitHubClient>(c =>
{
    c.BaseAddress = new Uri("https://api.github.com/");
    c.DefaultRequestHeaders.Add("Accept", "application/vnd.github.v3+json");
}).AddHeaderPropagation();

See samples/WebApplication.

Behaviour

HeaderPropagationOptions contains a dictionary where the key represent the name of the header to consume from the incoming request.

Each entry define the behaviour to propagate that header as follows:

  • OutboundHeaderName determines the name of the header to be used for the outbound http requests.

  • When present, the ValueFactory is the only method used to set the value. The factory should return StringValues.Empty to not add the header.

  • If multiple configurations for the same header are present, the first which returns a value wins.

Please note the factory is called only once per incoming request and the same value will be used by all the
outbound calls.

This is a backport to ASP.NET Core 2.1 (and 2.2) of the HeaderPropagation middleware I had recently contributed to the ASP.NET Core project.
All code is licensed under the Apache License, Version 2.0 and copyrighted by the .NET Foundation.

Motivation

I believe it is a common use case which deserves to be included in ASP.NET Core.
Its main use case is probably to track distributed transaction which requires the ability to pass through a transaction identifier as well as generating a new one when not present.

Given the ASP.NET Core 3.0 release is quite far away, and the current policy doesn't allow to backport new features to already shipped releases, I have created this package as recommended so it can be used today on projects based on ASP.NET Core 2.1 or 2.2.

Usage

In Startup.Configure enable the middleware:

app.UseHeaderPropagation();

In Startup.ConfigureServices add the required services, eventually specifying a configuration action:

services.AddHeaderPropagation(o =>
{
    // propagate the header if present
    o.Headers.Add("User-Agent");

    // if still missing, set it with a value factory
    o.Headers.Add("User-Agent", context => "Mozilla/5.0 (trust me, I'm really Mozilla!)");

    // propagate the header if present, using a different name in the outbound request
    o.Headers.Add("Accept-Language", "Lang");
});

If you are using the HttpClientFactory, add the DelegatingHandler to the client configuration using the AddHeaderPropagation extension method.

services.AddHttpClient<GitHubClient>(c =>
{
    c.BaseAddress = new Uri("https://api.github.com/");
    c.DefaultRequestHeaders.Add("Accept", "application/vnd.github.v3+json");
}).AddHeaderPropagation();

See samples/WebApplication.

Behaviour

HeaderPropagationOptions contains a dictionary where the key represent the name of the header to consume from the incoming request.

Each entry define the behaviour to propagate that header as follows:

  • OutboundHeaderName determines the name of the header to be used for the outbound http requests.

  • When present, the ValueFactory is the only method used to set the value. The factory should return StringValues.Empty to not add the header.

  • If multiple configurations for the same header are present, the first which returns a value wins.

Please note the factory is called only once per incoming request and the same value will be used by all the
outbound calls.

Release Notes

https://github.com/alefranz/HeaderPropagation/releases

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
3.0.1 1,724 5/30/2019
3.0.0 95 5/18/2019
2.0.0 1,472 5/8/2019
1.0.2 181 4/20/2019
1.0.1 3,146 4/7/2019
1.0.0 115 4/7/2019