WebApi 2:在消息处理程序中处理 OperationCanceledException 后的自定义 HttpResponseMessage 未返回给客户端
WebApi 2: Custom HttpResponseMessage after handling OperationCanceledException in Message Handler is not returned to client
我目前正在研究 WebApi 2 消息处理程序以及如何使用取消令牌实现服务器端超时。如果发生取消,则会抛出 OperationCanceledException 并在我的消息处理程序中进行处理。在这种情况下,我 return 具有足够 HttpStatusCode (HttpStatusCode.RequestTimeout) 的 HttpResponseMessage。
我希望我的消费客户端(使用邮递员)检索此 HttpStatusCode,但显示 "Could not get any response",因此我的客户端在没有任何其他信息的情况下中止。有人可以向我解释这种行为是怎么回事吗?我错过了什么?
参见以下示例代码:
public class RequestTimeoutHandler : DelegatingHandler
{
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
TimeSpan timeout = TimeSpan.FromSeconds(10.0);
using (CancellationTokenSource timeoutCancellationTokenSource = new CancellationTokenSource(timeout))
using (CancellationTokenSource linkedCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutCancellationTokenSource.Token))
{
try
{
return await base.SendAsync(request, linkedCancellationTokenSource.Token);
}
catch (OperationCanceledException e)
{
return new HttpResponseMessage(HttpStatusCode.RequestTimeout);
}
}
return null;
}
}
我的测试控制器方法如下所示:
[Route("testTimeoutAsyncHandleException"), HttpPost]
public IHttpActionResult TestTimeoutAsynchandle(string hugo, CancellationToken ct)
{
for (int i = 0; i < 500; i++)
{
Thread.Sleep(1000); //sleep 1 sec until exception is thown
if (ct.IsCancellationRequested)
{
ct.ThrowIfCancellationRequested();
}
}
return Ok("yes");
}
事实证明,使用 postman 并不是我最好的主意。我使用 httpclient 创建了一个控制台应用程序并请求了我的测试方法。 httpstatus 408 按预期返回。
我目前正在研究 WebApi 2 消息处理程序以及如何使用取消令牌实现服务器端超时。如果发生取消,则会抛出 OperationCanceledException 并在我的消息处理程序中进行处理。在这种情况下,我 return 具有足够 HttpStatusCode (HttpStatusCode.RequestTimeout) 的 HttpResponseMessage。 我希望我的消费客户端(使用邮递员)检索此 HttpStatusCode,但显示 "Could not get any response",因此我的客户端在没有任何其他信息的情况下中止。有人可以向我解释这种行为是怎么回事吗?我错过了什么?
参见以下示例代码:
public class RequestTimeoutHandler : DelegatingHandler
{
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
TimeSpan timeout = TimeSpan.FromSeconds(10.0);
using (CancellationTokenSource timeoutCancellationTokenSource = new CancellationTokenSource(timeout))
using (CancellationTokenSource linkedCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutCancellationTokenSource.Token))
{
try
{
return await base.SendAsync(request, linkedCancellationTokenSource.Token);
}
catch (OperationCanceledException e)
{
return new HttpResponseMessage(HttpStatusCode.RequestTimeout);
}
}
return null;
}
}
我的测试控制器方法如下所示:
[Route("testTimeoutAsyncHandleException"), HttpPost]
public IHttpActionResult TestTimeoutAsynchandle(string hugo, CancellationToken ct)
{
for (int i = 0; i < 500; i++)
{
Thread.Sleep(1000); //sleep 1 sec until exception is thown
if (ct.IsCancellationRequested)
{
ct.ThrowIfCancellationRequested();
}
}
return Ok("yes");
}
事实证明,使用 postman 并不是我最好的主意。我使用 httpclient 创建了一个控制台应用程序并请求了我的测试方法。 httpstatus 408 按预期返回。