In Asp.net, you can write a class that inherits ihttphandler, which has an isreusable attribute to indicate whether it can be reused.
My question is: Can httphandlers be reused during concurrency?
1 call Principle
The call principle of httphandler is that the resumesteps function of httpapplication first calculates which ihttphandler instance should be used to execute the request, and then calls the ihttphandler's processrequest method to execute the request.
Check the source code for reflection:
Execute function of maphandlerexecutionstep:
The last recyclehandlers () call of the resumesteps function is used to determine whether the ihttphandler instance can be reused next time:
Depending on the value of the isreusable attribute, decide whether to destroy the data (if the data is not destroyed, it will be used directly next time. If the data is destroyed, the data cannot be reused)
2. Can I reuse multiple concurrent requests?
Asp.net first requests httpapplication, and then httpapplication calls ihttphandler to execute the processrequest method.
If multiple requests are concurrently sent, multiple httpapplication instances will be created, so that the new httpapplication instance does not have the handler instance cache, so a new handler instance will be created.
Of course, after the two requests are completed, the httpapplication instance will be recycled and reused. The Handler instance that has been initialized will be used in the next request.
3 debugging proof:
- Prepare a handler and execute different actions for different input parameters:
public class MyCallBack : IHttpHandler{public bool IsReusable{get { return true; }}public void ProcessRequest(HttpContext context){string name = context.Request.QueryString["Method"];if (name == "Default") {Default();}else if (name == "Test"){Test();}else{}} public void Default(){Thread.Sleep(10000);HttpContext.Current.Response.Write("Default");}public void Test(){HttpContext.Current.Response.Write("Test");}}
2) Configure to Web. config
3) start debugging:
First request, instance: 6757235
The second request, instance or: 6757235, proves that the instance is reused.
In the third request, the instance is still: 6757235, And the execution continues to get stuck.
At the same time, a request is sent, and the result hits test smoothly.
However, the instance has changed because it was created by another httpapplication instance.
4 Conclusion:
Ihttphandler instance reuse is based on httpapplication. Even if it is reused, there is no guarantee that there is only one httphandler instance in the entire web application.
If a method is executed for a long time, it will not drag other requests because other requests work on another httpapplication instance. The requests that have been executed for a long time are not on the same instance.
Httphandlers instance Reuse