C # HttpHandler code explanation for asynchronous listener requests

Source: Internet
Author: User
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)!

  • Related Article

    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.