-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Description
Is there an existing issue for this?
- 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) 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<FooClient>().AddHeaderPropagation(o => o.Headers.Add("Authorization"));
services.AddHttpClient<BarClient>().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<FooClient>().AddHeaderPropagation(o => o.Headers.Add("Authorization"));
services.AddHttpClient<BarClient>().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:
- 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 theservices.AddHeaderPropagation()
call, but at least the not working method is hidden. - Change
AddHeaderPropagation(Action<HeaderPropagationMessageHandlerOptions>)
so that it adds the configured headers to the registeredHeaderPropagationOptions
(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