Skip to content

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

@jjanuszkiewicz

Description

@jjanuszkiewicz

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 HttpClients 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<HeaderPropagationMessageHandlerOptions>) 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-middlewareIncludes: URL rewrite, redirect, response cache/compression, session, and other general middlewares

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions