C# HttpHandler 异步监听请求的代码详解

黄舟
发布: 2017-03-03 11:28:41
原创
2029 人浏览过

在高并发下的服务器端编程,当遇到性能瓶颈时候,往往是同步带来的。监听HTTP请求的时候,异步是必须的。

异步监听HTTP请求的基类:

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
    {
        public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData)
        {
            _callback = 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()
        {
            //防止多次进行多次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; }
        }
    }
}
登录后复制

添加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();////最后别忘了end
            }


        }
    }
}
登录后复制

在站点引入MyHandler.dll,并按照如下修改 WebConfig:

<?xml version="1.0" encoding="utf-8"?>


<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0"/>
    <pages validateRequest="false" controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/>
    <httpRuntime requestValidationMode="2.0"/>
    <customErrors mode="Off"/>
    <httpHandlers>
      <add verb="*" path="Test.aspx" type="MyHandler.TestHandler, MyHandler"/>
    </httpHandlers>
  </system.web>


</configuration>
登录后复制

 以上就是C#   HttpHandler  异步监听请求的代码详解的内容,更多相关内容请关注PHP中文网(www.php.cn)!




来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板