【发布时间】:2016-11-24 13:23:20
【问题描述】:
我正在尝试实现一个自定义消息处理程序,下面是代码。
public class MyHandler : DelegatingHandler
{
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
{
/* Code that executes while receiving the request - start */
bool isBadrequest = false;
if (isBadrequest)
{
return request.CreateResponse(System.Net.HttpStatusCode.NotFound,"test");
}
/* Code that executes while receiving the request - end */
/* Code that executes while sending the response - start */
var response = await base.SendAsync(request , cancellationToken);
response.Headers.Add("new header", "new header value");
return response;
/* Code that executes while sending the response - end */
}
}
我知道 C# 中的任务和异步/等待。 但我无法理解在接收请求和发送响应时如何执行相同的方法。
【问题讨论】:
标签: asp.net asp.net-web-api asp.net-web-api2