I have a web API call that needs to make use of 2 delegating handlers, but I never seem to get in to the second handler. The route is registered with the handlers. I am not allowed to set the handlers in the global config.
DelegatingHandler[] handlers = new DelegatingHandler[2];
handlers[0] = new HandlerA();
handlers[1] = new HandlerB();
RouteTable.Routes.MapHttpRoute(
name: "TestAPI",
routeTemplate: "api/{controller}",
handler: HttpClientFactory.CreatePipeline(
innerHandler: new HttpClientHandler(),
handlers: handlers
),
defaults: new { controller = "test"},
constraints: null
);
HandlerA triggers a breakpoint without further problems.
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) {
if (request == null)
throw new ArgumentNullException("request");
//Breakpoint is hit
return base.SendAsync(request, cancellationToken);
}
I'd expected to arrive in the HandlerB but it never does, it goes straight to the API controller. Handler A doesn't do anything to the request.
I found the answer to my problem in this blog post: http://byterot.blogspot.nl/2012/05/aspnet-web-api-series-messagehandler.html
Instead of giving the API route the array of handlers, the first handler had the second handler set as inner handler.
DelegatingHandler[] handlers = new DelegatingHandler[2];
handlers[0] = new HandlerA();
handlers[1] = new HandlerB();
handlers[0].InnerHandler = handlers[1];
RouteTable.Routes.MapHttpRoute(
name: "TestAPI",
routeTemplate: "api/{controller}",
handler: handlers[0],
defaults: new { controller = "test"},
constraints: null
);