IIS can.. Net Site for resource control, including the CPU used and number of processes. however, if you want to impose a resource limit on some dynamic pages, you can only allow a fixed number of threads to process some dynamic requests, in some cases, dynamic requests will not occupy all resources of the entire site. this small granularity control is obviously not suitable for IIS. In this case, we can use Asp.net to provide ihttpasynchandler to solve this problem.
Processing Structure
Because Asp.net provides handler asynchronous processing, you can store specific objects in the queue in the handler's begin processing method, configure 1-n threads according to the actual business needs to process related requests.
Ihttpasynchandler
Public interface ihttpasynchandler: ihttphandler {// <summary> initiates an asynchronous call to the HTTP handler. </Summary> // <returns> An <see CREF = "T: system. iasyncresult "/> that contains information about the status of the process. </returns> // <Param name = "context"> An <see CREF = "T: system. web. httpcontext "/> object that provides references to intrinsic server objects (for example, request, response, session, and server) used to service HTTP requests. </param> // <Param name = "CB"> the <see CREF = "T: system. asynccallback "/> to call when the asynchronous method call is complete. if <paramref name = "CB"/> is null, the delegate is not called. </param> /// <Param name = "extradata"> any extra data needed to process the request. </param> iasyncresult beginprocessrequest (httpcontext context, asynccallback CB, object extradata); // <summary> provides an asynchronous process end method when the process ends. </Summary> // <Param name = "result"> An <see CREF = "T: system. iasyncresult "/> that contains information about the status of the process. </param> void endprocessrequest (iasyncresult result );}
SlaveCodeIhttpasynchandler is derived from ihttphandler and provides begin and end methods.
Asynchronously encapsulate existing pages
If ihttphandler is often used, it should be clear that this is used to describe a page request, including our aspx, and aspx processes an ihttphandlerfactory implementation system by default. web. UI. pagehandlerfactory. simply Inherit System. web. UI. pagehandlerfactory allows the original aspx request to return the corresponding asynchronous httphandler.
Public class custompagefactory: system. web. UI. pagehandlerfactory {static custompagefactory () {} public override ihttphandler gethandler (httpcontext context, string requesttype, string virtualpath, string path) {aspxasynchandler handler = new aspxasynchandler (base. gethandler (context, requesttype, virtualpath, PATH); Return handler ;}}
After pagefactory is implemented, you only need to simply configure a web. config file so that the existing aspx processing is processed by asynchronous handler.
<Handlers> <Add name = "custompage" verb = "*" Path = "*. aspx" type = "webapp. Code. custompagefactory, webapp"/>
Queue-based processing
The goal of creating a queue is to process tasks in an orderly and controllable manner.The execution of beginprocessrequest first stores the request in the column.
Public iasyncresult beginprocessrequest (httpcontext context, asynccallback CB, object extradata) {aspxasyncresult result = new convert (context, mhandler, CB); g_taskqueue.add (result); return result ;}
Develop thread processing
You can enable 1-n threads to process the work in the queue according to the actual situation.
Private void onrun (object state) {While (true) {aspxasyncresult asyncresult = POP (); If (asyncresult! = NULL) {asyncresult. Execute () ;}else {system. Threading. thread. Sleep (10 );}}}
The above is a fixed thread for processing, but this design is not good, that is, when there is no request, the thread will not stop doing sleep work, in fact, you can use the thread pool to complete according to the actual situation, it depends on the situation.
Summary
With the above design, you can easily control resources for certain page requests. If you are more concerned about the specific implementation, you can view
Http://blog.henryfan.net/post/2012/11/21/%E5% AE %9E%E7%8E%B0%E5%AF%B9%E7%8E%B0%E6%9C%89%E7%9A%84aspx%E8%AF%B7%E6%B1%82%E8%BF%9B%E8%A1%8C%E5%BC%82%E6%AD%A5%E9%98%9F%E5%88%97%E6%8E%A7%E5%88%B6%E5%A4%84%E7%90%86.aspx