【问题标题】:How many threads are involved in servicing a ASP.NET Web-API HTTP request?为 ASP.NET Web-API HTTP 请求提供服务涉及多少个线程?
【发布时间】:2014-06-16 15:09:02
【问题描述】:

我想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的不是一个线程吗?

有什么想法吗?

【问题讨论】:

  • 可能是在使用异步操作
  • @Jonesy:好的,那么有什么想法可以跟踪请求吗?

标签: c# asp.net .net asp.net-mvc asp.net-web-api


【解决方案1】:

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

请求可以“跳”线程,您仍然可以保证执行按顺序进行,但不能保证您始终在同一个线程上运行。

【讨论】:

  • 谢谢。但是,How to track a request ?
  • HttpContext.Items["id"] = new WhatYourIdIs() :)
  • @Yisahi Galatzer:所以,基本上我们是在 cookie 容器中传递 unique id ?正确吗?
  • 不,我们的想法是我们在一个属性包中传递它,该属性包与请求一起跳线程
猜你喜欢
  • 2015-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多