Server-side programming under high concurrency, when encountering performance bottlenecks, is often the result of synchronization. Asynchronous is required when listening for HTTP requests.
Base class for asynchronous listener HTTP requests:
Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading;using System.web;namespace myhandler{Public abstract class Httpasynchandler:ihttpasynchandler, IAsyncResult {PU Blic IAsyncResult BeginProcessRequest (HttpContext context, AsyncCallback cb, Object Extradata) {_callba CK = CB; _context = context; _completed = false; _state = this; ThreadPool.QueueUserWorkItem (New WaitCallback (doprocess), this); return this; } public void EndProcessRequest (IAsyncResult result) {} public bool IsReusable { get {return false;} } public abstract void Beginprocess (HttpContext context); public void Endprocess () {//Prevents multiple endprocess if (!_completed) { try {_completed = true; if (_callbacK! = null) {_callback (this); }} catch (Exception) {}}} private static void Doprocess (object State) {Httpasynchandler handler = (Httpasynchandler) state; Handler. Beginprocess (Handler._context); } public void ProcessRequest (HttpContext context) {throw new NotImplementedException (); } private bool _completed; Private Object _state; Private AsyncCallback _callback; Private HttpContext _context; Public object AsyncState {get {return _state;} } public WaitHandle AsyncWaitHandle {get {throw new NotImplementedException ();} } public bool completedsynchronously {get {return false;} } public bool IsCompleted {get {return _completed;} } }}
Add TestHandler.cs:
Using system;using system.collections.generic;using system.linq;using system.text;using System.IO;namespace myhandler{public class Testhandler:httpasynchandler {public override void Beginprocess ( System.Web.HttpContext context) { try { StreamReader sr = new StreamReader (context. Request.inputstream); String reqstr = Sr. ReadToEnd (); Context. Response.Write ("Get your input:" + Reqstr + "at" + DateTime.Now.ToString ()); } catch (Exception ex) { context. Response.Write ("Exception eccurs ex info:" + ex.) Message); } Finally { endprocess ();////finally don't forget end } }}}
Introduce MyHandler.dll at the site and modify the Webconfig as follows:
<?xml version= "1.0" encoding= "Utf-8"?><configuration> <system.web> <compilation Debug= "true" targetframework= "4.0"/> <pages validaterequest= "false" controlrenderingcompatibilityversion= "3.5" clientidmode= "Autoid"/>
The above is the C # HttpHandler asynchronous listening request code detailed content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!