Httphandlers instance Reuse

Source: Internet
Author: User

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:

  1. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.