I have a web API message handler MyHandler
that I want to run in OWIN pipeline as a middleware. So configuring the handler like this.
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseHttpMessageHandler(new MyHandler());
HttpConfiguration config = new HttpConfiguration();
config.Routes.MapHttpRoute(
"DefaultWebApi",
"{controller}/{id}",
new { id = RouteParameter.Optional });
app.UseWebApi(config);
}
}
Handler is very simple and does nothing.
public class MyHandler : DelegatingHandler
{
protected override async Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request, CancellationToken cancellationToken)
{ // <--- breakpoint here
var response = await base.SendAsync(request, cancellationToken);
return response;
}
}
I put a break point inside SendAsync
and it does break but the following base.SendAsync
bombs silently and I see A first chance exception of type 'System.InvalidOperationException' occurred in System.Net.Http.dll
.
I can quite easily add MyHandler
to config.MessageHandlers
and it will run perfect in the Web API pipeline but that's not what I want to do. I want to run MyHandler
in the OWIN pipeline. Is this possible at all? It should be. Otherwise, there is no point in having the extension method UseHttpMessageHandler
, I guess. Just that I couldn't figure out a way to do what I want to do.