Using Func<>’s
When directly creating a RestClient instance then simply pass one or more Func<>’s to the Config constructor.
This example uses two Func<>’s to add headers to the request:
dynamic restClient = new RestClient("http://headers.jsontest.com/",
new Config
(
async (request, token, next) =>
{
request.Headers.Add("TestHandlerHeader1", "TestHandler1");
return await next(request, token); //Call next handler in pipeline
},
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(async (request, token, next) =>
{
request.Headers.Add("TestHandlerHeader1", "TestHandler1");
return await next(request, token); //Call next handler in pipeline
})
.UseHandler(async (request, token, next) =>
{
request.Headers.Add("TestHandlerHeader2", "TestHandler2");
return await next(request, token); //Call next handler in pipeline
});