DEV Community

loading...

Discussion on: Make SOAP requests using IHttpClientFactory in .NET Core

Collapse
nikolicbojan profile image
Bojan Nikolić Author

You can add them through additional DelegatingHandler which you would add to the HttpClient setup. So, create something like TraceLogHandler and append a header.
So, something like

internal class HeaderHandler : DelegatingHandler
{
    protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        request.Headers.Add("X-MyHeader", "MyValue");

        return base.SendAsync(request, cancellationToken);
    }
}
Enter fullscreen mode Exit fullscreen mode

Then in Startup.cs first register it

services.AddScoped<HeaderHandler>();
Enter fullscreen mode Exit fullscreen mode

and then add it to the HttpClient setup e.g. before AddHeaderPropagation line

.AddHttpMessageHandler<HeaderHandler>()
Enter fullscreen mode Exit fullscreen mode

Let me know if this is not what you had in mind or it doesn't work for your case.

Forem Open with the Forem app