![](/img/trans.png)
[英]Synchronous Web Api and response time depending on the thread and request count
[英]Web API response time
我正在构建服务器监视系统,并希望将请求发送到Web API,并通过JSON对象(如果数据库连接正常,响应时间等)获取服务器的运行状况。
如何实现响应时间,说Web API回答请求需要多长时间?
如果要实施Web Api监视,则可以创建自定义DelegatingHandler来跟踪操作持续时间和状态。
这是一个测量操作持续时间的非常基本的示例。 持续时间被添加到响应中(相当无用); 最好将这类数据存储到专用存储库中。
public class MonitoringDelegate : DelegatingHandler
{
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
CancellationToken cancellationToken)
{
var watcher = Stopwatch.StartNew();
var response = await base.SendAsync(request, cancellationToken);
watcher.Stop();
//store duration somewheren here in the response header
response.Headers.Add("X-Duration", watcher.ElapsedMilliseconds.ToString());
return response;
}
}
您可以在客户端启动一个秒表 ,并在返回篷房时将其停止
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.