服务一个ASP涉及多少线程?. NET Web-API HTTP请求

本文关键字:线程 多少 NET Web-API 请求 HTTP ASP 一个 服务 | 更新日期: 2023-09-27 18:08:53

我认为log Thread-ID是为了跟踪请求并使日志文件可读。

所以,我最初的尝试是这样的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);
}

但是当我看到日志文件时,它很奇怪。

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()

Application_Start中,Thread-Id286,但当它回到Application_EndRequest时,Thread-Id changed to 287

不是一个线程服务整个HTTP request吗?

服务一个ASP涉及多少线程?. NET Web-API HTTP请求

请求是单线程处理的,但这并不意味着在整个请求过程中都是同一个线程。

请求可以"跳转"线程,您仍然可以保证执行顺序发生,但不能保证始终在同一线程上运行。