stevejgordon / CorrelationId

An ASP.NET Core middleware component which synchronises a correlation ID for cross API request logging.
MIT License
558 stars 106 forks source link

Automatically add "X-Correlation-ID" to outgoing HTTP Client request via HttpClientFactory & DelegatingHandlers #31

Closed madhub closed 4 years ago

madhub commented 5 years ago

I was reading your articles of HttpClientFactory usage in AspNet Core 2.1 , I was thinking we can add "X-Correlation-ID" header automatically to outgoing HTTP Client request via HttpClientFactory& DelegatingHandlers. Is this a possibility ?

efirdman commented 5 years ago

Same here. I found your example:

        var builder = new WebHostBuilder()
           .Configure(app => app.UseCorrelationId())
           .ConfigureServices(sc => sc.AddCorrelationId());
        var server = new TestServer(builder);
        var response = server.CreateClient().GetAsync("").GetAwaiter().GetResult();
        var expectedHeaderName = new CorrelationIdOptions().Header;
        var header = response.Headers.GetValues(expectedHeaderName);

However, I don't get how I can set up my HttpClientFactory to include Correlation ID in all my outgoing HTTP Client requests.

ajtowf commented 5 years ago

Consider using header propagation instead; https://gist.github.com/davidfowl/c34633f1ddc519f030a1c0c5abe8e867

Just a thought :-)

stevejgordon commented 4 years ago

@ajtowf Agree that is a good option. I've added a feature to support basic header forwarding directly from this library if people prefer to use that.