IHttpAsyncHandler 的一个DEMO

    public class IHttpAsyncHandlerDEMO : IHttpHandler, IHttpAsyncHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Write("Hello World");
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData)
        {
            if (string.IsNullOrEmpty(context.Request.QueryString["IsAdd"]))
            {
                return new CustomIAsyncResult();
            }

            SqlConnection con = new SqlConnection("server=xxx;uid=xxx;pwd=xxx;database=Test;Asynchronous Processing=true");
            SqlCommand cmd = new SqlCommand("insert into xxx values(1,1,1)", con);
            con.Open();
            return cmd.BeginExecuteNonQuery(cb, new CustomAsyncState { cmd = cmd, context = context });

        }

        public void EndProcessRequest(IAsyncResult result)
        {
            if (result is CustomIAsyncResult)
            {
                HttpContext.Current.Response.Write("无效参数!");
            }
            else
            {
                //result.AsyncState
                CustomAsyncState state = result.AsyncState as CustomAsyncState;
                if (state != null)
                {
                    int count = 0;
                    if (state.cmd != null)
                    {
                        count = state.cmd.EndExecuteNonQuery(result);
                        if (state.cmd.Connection != null)
                        {
                            state.cmd.Connection.Close();
                            state.cmd.Connection.Dispose();
                        }
                    }

                    if (state.context != null)
                    {
                        state.context.Response.Write(string.Format("增加{0}条记录", count));
                    }
                }
            }
        }
    }

    internal class CustomAsyncState
    {
        internal HttpContext context { set; get; }
        internal SqlCommand cmd { set; get; }
    }

    internal class CustomIAsyncResult : IAsyncResult
    {

        public object AsyncState
        {
            get { return ""; }
        }

        public System.Threading.WaitHandle AsyncWaitHandle
        {
            get { throw new NotImplementedException(); }
        }

        public bool CompletedSynchronously
        {
            get { return true; }
        }

        public bool IsCompleted
        {
            get { return true; }
        }
    }


IHttpAsyncHandlerDEMO.ashx  -> 无效参数!

IHttpAsyncHandlerDEMO.ashx?IsAdd  -> 无效参数!

IHttpAsyncHandlerDEMO.ashx?IsAdd=2  -> 增加x条记录

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#网页版+客户端版聊天软件源码分享(C#+长连接+Sqllite数据库实现) 今天我给大家分享一个聊天程序的源码。 网页版加客户端版并存,可以互通 我相信对大家学习和扩展这一块知识是很有用的。 我们先来看下软件结构 一个Web版一个网页版,而客户端是连接的网页的 http://localhost:53947/wwwroot/Lesktop 这个路径 http://localhost:53947/wwwroot/这一部分是网页的地址,大家可以根据自己配置情况进行修改 然后浏览一下Default.aspx页面如下 这是负面版的,客户端的也是一样的,我们先来注册 一个账户 在这里我们注册两个账户还有一个是text用来聊天对话使用 注册的方法是一样的我就不多说了。 下面登录第一个账户看看 这是登录后的效果。 单击聊天室 然后我们再登录另外一个账户text 好了大家应该能看到效果了吧。 然后咱们再发个“你好” 收到了吧,再回复一个 对就是这个效果, 再来看看桌面版的 刚才的消息都在 这是桌面版的效果。 大家感觉怎么样。 我感觉大家可以在这个基础之上进行扩展,最少可以看看他的实现思路 源码分享给大家了 sufeinet.com即时通信_云骞.zip (3.25 MB, 下载次数: 1078) ReceiveResponsesHandler 类,这个主要是用来接收和维护长连接的 实现长连接的两个重要来代码预览 [C#] 纯文本查看 复制代码 ? 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 using System; using System.Collections.Generic; using System.Collections; using System.Text; using System.Web; using System.Xml; using System.Threading; namespace Core { public class ReceiveResponsesHandler : IHttpAsyncHandler { public ReceiveResponsesHandler() { } HttpContext m_Context = null; IAsyncResult IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) { m_Context = context; string sessionId = context.Request.Params["SessionID"]; string clientVersion = context.Request.Params["ClientVersion"]; string serverVersion = context.Request.Params["ServerVersion"]; ResponsesListener asyncResult = new ResponsesListener(sessionId, cb, extraData); try { if (serverVersion != ServerImpl.Instance.Version) throw new IncompatibleException();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值