Mixing Func<>’s and DelegatingHandlers
When directly creating a RestClient instance use the DelegatingHandlerWrapper if you need to mix Func<>’s and DelegatingHandlers:
dynamic restClient = new RestClient("http://headers.jsontest.com/",
new Config
(
new MyHandler1(),
new DelegatingHandlerWrapper(async (request, token, next) =>
{
request.Headers.Add("TestHandlerHeader2", "TestHandler2");
return await next(request, token); //Call next handler in pipeline
})
));
When using IHttpClientFactory use the UseHandler extension method when adding the RestClient to your .NET Core DI container (usually in Startup.cs):
services
.AddRestClient("http://headers.jsontest.com/")
.UseHandler(()=> new MyHandler1())
.UseHandler(async (request, token, next) =>
{
request.Headers.Add("TestHandlerHeader2", "TestHandler2");
return await next(request, token); //Call next handler in pipeline
});