DelegatingHandler在响应的WebAPI [英] DelegatingHandler for response in WebApi
问题描述
我目前使用的几个代表团处理器(从派生类 DelegatingHandler
),以对工作的要求发送之前,对于像验证签名等,这是所有很不错,因为我没有重复签名验证的所有电话(例如)。
I am currently using several delegation handlers (classes derived from DelegatingHandler
) to work on the request before it is sent, for things like validating a signature etc. This is all very nice, because I don't have to duplicate signature validation on all calls (for example).
我想用从同一Web请求的响应同样的原则。是否有类似DelegatingHandler的响应的东西?办法赶上响应之前就已经回到了方法,在某种程度上?
I would like to use the same principle on the response from the same web request. Is there something similar to the DelegatingHandler for the response? A way to catch the response before it has returned to the method, in a way?
更多信息:
我使用调用Web API HttpClient.PutAsync(...)
推荐答案
是的。你可以做,在延续任务。
Yes. You can do that in the continuation task.
我解释一下这里。
例如,这code(从博客上面)的痕迹请求的URI,并增加了一个虚拟头的响应。
For example, this code (from the blog above) traces request URI and adds a dummy header to response.
public class DummyHandler : DelegatingHandler
{
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
// work on the request
Trace.WriteLine(request.RequestUri.ToString());
return base.SendAsync(request, cancellationToken)
.ContinueWith(task =>
{
// work on the response
var response = task.Result;
response.Headers.Add("X-Dummy-Header", Guid.NewGuid().ToString());
return response;
});
}
}
这篇关于DelegatingHandler在响应的WebAPI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!