我有一个Web API消息处理程序MyHandler,我想作为中间件在OWIN管道中运行。因此,像这样配置处理程序。

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);
    }
}

处理程序非常简单,不执行任何操作。
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;
    }
}

我在SendAsync内放了一个断点,它确实断了,但是下面的base.SendAsync炸弹无声地炸了,我看到了A first chance exception of type 'System.InvalidOperationException' occurred in System.Net.Http.dll

我可以很容易地将MyHandler添加到config.MessageHandlers,它将在Web API管道中完美运行,但这不是我想要的。我想在OWIN管道中运行MyHandler。这有可能吗?它应该是。否则,我想使用扩展方法UseHttpMessageHandler是没有意义的。只是我想不出一种方法去做自己想做的事。

最佳答案

是的,需要改进这种体验,因为会默默地忽略异常。

对于上述情况,您将需要派生自HttpMessageHandler而不是DelegatingHandler,因为委托(delegate)处理程序将尝试将请求委托(delegate)给处理程序之后的示例(例如:异常提到Message=The inner handler has not been assigned)

例如,以下将起作用:

appBuilder.UseHttpMessageHandler(new MyNonDelegatingHandler());

public class MyNonDelegatingHandler : HttpMessageHandler
{
    protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        HttpResponseMessage response = new HttpResponseMessage();
        response.Content = new StringContent("Hello!");

        return Task.FromResult<HttpResponseMessage>(response);
    }
}

为了创建处理程序链,您可以执行以下操作:
appBuilder.UseHttpMessageHandler(HttpClientFactory.CreatePipeline(innerHandler: new MyNonDelegatingMessageHandler(),
           handlers: new DelegatingHandler[] { new DelegatingHandlerA(), new DelegatingHandlerB() }));

10-05 21:36