dotnet / dotnet/aspnetcore

`AddHeaderPropagation()` without providing `Action<HeaderPropagationOptions> configureOptions` prevents middleware from working correctly

Open
#48,581 8 comments 3 reactions 0 assignees View on GitHub
area-middleware
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 5h
Merged PRs (30d)
276

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Describe the bug

When trying to configure the header propagation feature using the `HeaderPropagationServiceCollectionExtensions.AddHeaderPropagation(this IServiceCollection services)` (
[link](https://github.com/dotnet/aspnetcore/blob/d919168952ac4da33be8532c6980547a783a7389/src/Middleware/HeaderPropagation/src/DependencyInjection/HeaderPropagationServiceCollectionExtensions.cs#LL20C6-L20C6)) method, there is no `HeaderPropagationOptions` instance registered, so the middleware doesn't capture any headers and header propagation won't work, even when header names are specified for a specific `HttpClient`, e.g.:

services.AddHeaderPropagation();
services.AddHttpClient().AddHeaderPropagation(o => o.Headers.Add("Authorization"));
services.AddHttpClient().AddHeaderPropagation(o => o.Headers.Add("X-Another-Header"));

This results in no headers being forwarded.

Instead, this needs to be done:

services.AddHeaderPropagation(o =>
{
o.Headers.Add("Authorization");
o.Headers.Add("X-Another-Header");
});
services.AddHttpClient().AddHeaderPropagation(o => o.Headers.Add("Authorization"));
services.AddHttpClient().AddHeaderPropagation(o => o.Headers.Add("X-Another-Header"));

This is especially problematic if different `HttpClient`s require forwarding different headers - the `services.AddHeaderPropagation` call needs to list all those headers, which is code duplication and leads to bugs.

I propose to either:
1. Make `AddHeaderPropagation(this IServiceCollection services)` internal, as it's useless for users of this library. This way all headers still need to be listed explicitly in the `services.AddHeaderPropagation()` call, but at least the not working method is hidden.
2. Change `AddHeaderPropagation(Action)` so that it adds the configured headers to the registered `HeaderPropagationOptions` (and/or registers it if it's not yet registered).

I can create a PR for either of these (I prefer option 2), but looking for some feedback first.

### Expected Behavior

_No response_

### Steps To Reproduce

_No response_

### Exceptions (if any)

_No response_

### .NET Version

_No response_

### Anything else?

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.