【发布时间】:2018-09-05 23:15:47
【问题描述】:
我有一个 Web API C# 项目。我注意到,当一起向控制器发送几个请求(很少甚至 7-10 个)时,一些请求需要很长时间(5-7 秒)。单独发送每个请求时,每个请求耗时不到 200ms。我正在向我的本地主机(开发环境)发送请求,因此服务器上不应该有任何延迟或大量使用。
我在 global.asax 中添加了这段代码,以便查看每个请求需要多长时间。
//Global asax
private Dictionary<string, StopWatch> urlTimers = new Dictionary<string, StopWatch>();
void Application_BeginRequest(object sender, EventArgs e)
{
HttpContextBase currentContext = new HttpContextWrapper(HttpContext.Current);
var requestedUrl = currentContext.Request.RequestContext.HttpContext.Request.RawUrl;
var urlWithoutQueryParams = requestedUrl.Split('?')[0];
if (urlWithoutQueryParams.StartsWith("/controller"))
{
var stopWatch = new StopWatch();
stopWatch.Start(urlWithoutQueryParams);
urlTimers[urlWithoutQueryParams] = stopWatch;
}
}
void Application_EndRequest(object sender, EventArgs e)
{
HttpContextBase currentContext = new HttpContextWrapper(HttpContext.Current);
var requestedUrl = currentContext.Request.RequestContext.HttpContext.Request.RawUrl;
var urlWithoutQueryParams = requestedUrl.Split('?')[0];
if (urlWithoutQueryParams.StartsWith("/controller"))
{
var stopWatch = urlTimers[urlWithoutQueryParams];
if (stopWatch != null)
{
stopWatch.Stop();
}
}
}
当使用提琴手提交 10 个请求时,我收到以下内容
所有请求都返回少量数据,所以这也不应该是问题。 10 个请求花费这么多时间是没有意义的。任何帮助将不胜感激。
【问题讨论】:
标签: c# asp.net asp.net-web-api