我有一個Web API消息處理程序MyHandler
,我想在OWIN管道中作爲中間件運行。因此像這樣配置處理程序。將HttpRequestMessage轉換爲OwinRequest和OwinResponse以HttpResponseMessage
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
毫無意義。只是我無法想出辦法去做我想做的事。
我認爲你混淆了消息處理程序和委託處理程序的概念。消息處理程序只是接受請求並返回響應的東西。 –
我明白消息處理程序和委託處理程序之間的區別。委託處理程序是一個消息處理程序,我只是希望能夠使用委託處理程序來代替消息處理程序,並且神奇地它將在OWIN管道中運行,就像它在Web API管道中運行一樣,顯然這是不可能的,基於Kiran的答覆。我同意我太雄心勃勃。感謝您看我的問題。 – Badri