简体   繁体   English

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

[英]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_StartThread-Id286但回到Application_EndRequestThread-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.

相关问题 ASP.NET Web-API OWIN:找不到与请求URI匹配的HTTP资源 - ASP.NET Web-API OWIN: No HTTP resource was found that matches the request URI 如何在ASP.net Web-API中使用CustomExceptionException处理程序 - How to use customexceptionhandlers in ASP.net web-api asp.net Web-Api JSON字符串反序列化 - asp.net Web-Api JSON string deserialization Asp.net为web-api解雇重定向的核心授权 - Asp.net core authorization for web-api firing redirect 如何引用已经在 API 级别声明的 Object? (asp.net web-api) - How to reference Object that it has been already declared at the API level? (asp.net web-api) ASP.net Web-api中的简单类型JSON序列化 - Simple types JSON serialization in ASP.net Web-api 如何通过ID发送消息到signalR特定的PersistentConnection? (使用asp.net mvc4 web-api) - How to send message to signalR specific PersistentConnection by its ID ? (with asp.net mvc4 web-api) 如何从ASP.NET Web-API中的另一个方法调用Windows中的方法 - how to call a method in a windows from another method in asp.net web-api 如何在ASP.NET Web-API项目中将FluentValidation与LightInject一起使用 - How to use FluentValidation with LightInject in asp.net web-api project 如何接收发布到 ASP.net 核心 2.1 Web-API 端点的文件和数据 - How to receive both File and data POSTed to an ASP.net core 2.1 Web-API endpoint
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM