[英]How many threads are involved in servicing a ASP.NET Web-API HTTP request?
I thought to log Thread-ID
for the purpose of tracking a request and make the log file readable. 我认为
log Thread-ID
的目的是跟踪请求并使日志文件可读。
So, my initial attempts were logging in Global.asax
like this: 因此,我最初的尝试是这样
logging in Global.asax
:
protected void Application_BeginRequest()
{
log4net.ILog writer = log4net.LogManager.GetLogger(this.GetType());
writer.InfoFormat("Gotta service a request. Id = {0}", System.Threading.Thread.CurrentThread.ManagedThreadId);
}
protected void Application_EndRequest()
{
log4net.ILog writer = log4net.LogManager.GetLogger(this.GetType());
writer.InfoFormat("Completed servicing a request. Id = {0}", System.Threading.Thread.CurrentThread.ManagedThreadId);
}
But when I see the log-file, it is very strange. 但是,当我看到日志文件时,这很奇怪。
2014-06-16 20:27:06,018 - Gotta service a request. Id = 286 --> From Application_BeginRequest()
2014-06-16 20:27:06,042 - Servicing Request Thread-Id = 286 --> this message comes from controller
2014-06-16 20:27:06,043 - Thread-Id = 286 --> this message comes from controller
2014-06-16 20:27:06,237 - Completed servicing a request. Id = 287 --> From Application_EndRequest()
In Application_Start
, the Thread-Id
is 286
but when it comes back to Application_EndRequest
, the Thread-Id changed to 287
. 在
Application_Start
, Thread-Id
为286
但回到Application_EndRequest
, Thread-Id changed to 287
。
Is it not one thread that services the entire HTTP request
? 是不是一个线程为整个
HTTP request
提供服务?
Any ideas why ? 有什么想法吗?
The request is processed single threaded, but that does not mean it is the same thread throughout the request. 该请求是单线程处理的,但这并不意味着它是整个请求中的同一线程。
The request can "hop" threads, you are still guaranteed the execution happens in order but you are not guaranteed to run on the same thread throughout. 该请求可以“跳跃”线程,仍然可以保证顺序执行,但是不能保证始终在同一线程上运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.