Httphandler学习中的知识点.
2007-07-03 09:42
309 查看
以下内容均来自于网络和msdn.
个人理解httphandler是为了创建自定义后缀类型文件按照aspx来解析的一种形式.
以下为收集的一些资料和例子:
ASP.NET 中的内置 HTTP 处理程序
ASP.NET 根据文件扩展名将 HTTP 请求映射到 HTTP 处理程序。每个 HTTP 处理程序都能够处理应用程序中的单个 HTTP URL 或 URL 扩展名组。ASP.NET 包括几种内置的 HTTP 处理程序,如下表所列。
创建自定义 HTTP 处理程序
若要创建一个自定义 HTTP 处理程序,可以创建一个可实现 IHttpHandler 接口的类以创建同步处理程序,或者创建一个可实现 IHttpAsyncHandler 的类以创建异步处理程序。两种处理程序接口都要求您实现 IsReusable 属性和 ProcessRequest 方法。IsReusable 属性指定 IHttpHandlerFactory 对象(实际调用适当处理程序的对象)是否可以将您的处理程序放置在池中,并且重新使用它们以提高性能,或是否在每次需要处理程序时都必须创建新实例。ProcessRequest 方法负责实际处理单个 HTTP 请求。
创建文件扩展名
创建一个类文件作为您的 HTTP 处理程序时,可以让您的处理程序响应尚未在 IIS 和 ASP.NET 中映射的任何文件扩展名。例如,如果您在创建用于生成 RSS 源的 HTTP 处理程序,则可以将处理程序映射到扩展名为 .rss的文件。为了让 ASP.NET 知道哪个处理程序将用于您的自定义扩展名文件,必须在 IIS 中将处理程序类文件的扩展名映射到 ASP.NET,并且在您的应用程序中将该扩展名映射到您的自定义处理程序。
默认情况下,ASP.NET 为自定义 HTTP 处理程序映射文件扩展名 .ashx 的方式与将扩展名 .aspx 映射到 ASP.NET 页处理程序的方式相同。因此,如果您创建具有文件扩展名 .ashx 的 HTTP 处理程序类,该处理程序将自动注册到 IIS 和 ASP.NET。
如果想要为您的处理程序创建自定义文件扩展名,则必须显式将该扩展名注册到 IIS 和 ASP.NET。不使用文件扩展名 .ashx 的好处是您的处理程序随后可以重新用于其他扩展名映射。例如,在某个应用程序中,您的自定义处理程序可能响应以 .rss 结尾的请求,而在另一个应用程序中,您的自定义处理程序可能响应以 .feed 结尾的请求。再举一例,您的处理程序可能映射到同一应用程序中的两个文件扩展名,但可能基于扩展名创建两个不同的响应。
异步 HTTP 处理程序
利用异步 HTTP 处理程序可以启动一个外部进程(例如对远程服务器的方法调用),然后继续处理程序的处理工作,而无需等待外部进程结束。在异步 HTTP 处理程序的处理期间,ASP.NET 将通常用于外部进程的线程放回线程池中,直到处理程序收到来自外部进程的回调。这样可以避免阻止线程,并大幅改善了性能,因为一次所能执行的线程数量是有限的。如果许多用户都在请求依赖于外部进程的同步 HTTP 处理程序,那么操作系统可能很快就会用完所有线程,因为大量线程被阻止,正在等待外部进程。
创建异步处理程序时,除了实现 IHttpAsyncHandler 接口,还必须实现 BeginProcessRequest 以启动异步调用来处理单个 HTTP 请求。还必须实现 EndProcessRequest 方法,以便在进程结束时运行清理代码。
自定义 IHttpHandlerFactory 类
IHttpHandlerFactory 类接收请求并负责向相应的 HTTP 处理程序转发请求。您可以通过创建一个实现了 IHttpHandlerFactory 接口的类来创建自定义 HTTP 处理程序工厂。创建自定义处理程序工厂可以更好地控制对 HTTP 请求的处理,因为这样可以基于运行时条件创建不同的处理程序。例如,使用自定义 HTTP 处理程序工厂,可以在 HTTP 请求方法为 PUT 时为某个文件类型实例化一个 HTTP 处理程序,而在该方法为 GET 时实例化另一个 HTTP 处理程序。
以上为一段基础知识.如果要创建一个自定义的扩展名网页,而且要求该网页是收asp.net控的网页必须在iis和asp.net中进行注册.从而自定义扩展名的网页就有了和后缀为.aspx的网页一样的HTTP处理机制.
写个同步调用的例子:
新建一个网站-->在App_code中添加一个HelloWorldHandler类代码如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Data;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Configuration;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web.Security;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web.UI;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web.UI.WebControls;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web.UI.WebControls.WebParts;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web.UI.HtmlControls;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**//// <summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// HelloWorldHandler 的摘要说明
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
/// </summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public class HelloWorldHandler : IHttpHandler
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public HelloWorldHandler()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// TODO: 在此处添加构造函数逻辑
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
IHttpHandler 成员#region IHttpHandler 成员
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public bool IsReusable
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
get ...{ return false; }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void ProcessRequest(HttpContext context)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HttpRequest httpRequest = context.Request;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HttpResponse httpResponse = context.Response;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
httpResponse.Write("<html>");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
httpResponse.Write("<body>");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
httpResponse.Write("<h1>Hello from a synchronous custom HTTP handler.</h1>");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
httpResponse.Write("</body>");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
httpResponse.Write("</html>");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
#endregion
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
在web.config文件中添加如下注册信息:
<system.web>
<httpHandlers>
<add verb="*" path="*.sample" type="HelloWorldHandler"/>
</httpHandlers>
</system.web>
在我们创建的网站中增加一个Test.sample文件.
在iis中配置映射关系:
创建网站-->选择处理程序映射 如下图:
![](http://p.blog.csdn.net/images/p_blog_csdn_net/winnerzone/httphandle1.jpg)
双击进入处理程序映射-->选择添加脚本映射 如下图:
![](http://p.blog.csdn.net/images/p_blog_csdn_net/winnerzone/httphandle2.jpg)
请求路径填写*.sample
可执行文件选择 Microsoft.NET/Framework/v2.0.50727/aspnet_isapi.dll 这个文件.
名称填写.sample
![](http://p.blog.csdn.net/images/p_blog_csdn_net/winnerzone/httphandle3.jpg)
确定.确定.我们打开我们自己创建的Test.sample文件看下?效果如下图:
![](http://p.blog.csdn.net/images/p_blog_csdn_net/winnerzone/httphandle4.jpg)
iis配置与同步调用相同.
web.config中的属性节点的说明
1、verb属性说明了该handle所支持的http请求方式,例如支持post和get方式,verb属性则为"POST,GET";如果支持所有的请求方式,verb属性则用"*"。
2、 path属性说明了对哪些文件的请求才调用该handle来处理,例如你只想在请求my.sample文件时才调用该handle,则path属性为 "my.sample",如果你想所有后缀名为sample的文件(*.possible)都由该handle来处理,则path属性为 "*.sample"。
3、type属性中指定了handle类的命名空间、类名和工程名。ASP.NET runtime会首先到应用程序的bin目录下查找该配件的dll,如果没有找到再到GAC里查找。
因为我使用的是vista所以配置可能和iis6.0不一样,下面是msdn上给的iis6.0的配置说明:
打开“Internet 信息服务(IIS)管理器”。
右击应用程序的名称,然后单击“属性”。
单击“虚拟目录”选项卡,然后单击“配置”。
在“映射”选项卡上,单击“添加”。
将显示“添加/编辑应用程序扩展名映射”对话框。
在“可执行文件”框中,键入或浏览至 Aspnet_isapi.dll 文件。默认情况下,该文件位于以下位置。
%windows%/Microsoft.NET/Framework/version/
在“扩展名”框中,键入 .sample。
清除“确认文件是否存在”复选框。
单击“确定”,然后关闭 IIS 管理器。
下面来创建个异步调用的例子.代码如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Data;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Configuration;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web.Security;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web.UI;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web.UI.WebControls;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web.UI.WebControls.WebParts;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web.UI.HtmlControls;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Threading;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**//// <summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// HelloWorldAsyncHandler 的摘要说明
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
/// </summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public class HelloWorldAsyncHandler : IHttpAsyncHandler
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public HelloWorldAsyncHandler()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// TODO: 在此处添加构造函数逻辑
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
IHttpAsyncHandler 成员#region IHttpAsyncHandler 成员
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
context.Response.Write("<p>Begin IsThreadPoolThread is " + Thread.CurrentThread.IsThreadPoolThread + "</p> ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
AsynchOperation asynch = new AsynchOperation(cb, context, extraData);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
asynch.StartAsyncWork();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return asynch;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void EndProcessRequest(IAsyncResult result)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
#endregion
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
IHttpHandler 成员#region IHttpHandler 成员
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public bool IsReusable
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
get ...{ return false; }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void ProcessRequest(HttpContext context)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
throw new InvalidOperationException();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
#endregion
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class AsynchOperation : IAsyncResult
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private bool _completed;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private Object _state;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private AsyncCallback _callback;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private HttpContext _context;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//// <summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 是否成功
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
/// </summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
bool IAsyncResult.IsCompleted ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
get
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return _completed;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
WaitHandle IAsyncResult.AsyncWaitHandle
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
get
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Object IAsyncResult.AsyncState
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
get
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return _state;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bool IAsyncResult.CompletedSynchronously
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
get
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public AsynchOperation(AsyncCallback callback, HttpContext context, Object state)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_callback = callback;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_context = context;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_state = state;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_completed = false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void StartAsyncWork()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ThreadPool.QueueUserWorkItem(new WaitCallback(StartAsyncTask), null);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private void StartAsyncTask(Object workItemState)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_context.Response.Write("<p>Completion IsThreadPoolThread is " + Thread.CurrentThread.IsThreadPoolThread + "</p> ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_context.Response.Write("Hello World from Async Handler!");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_completed = true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_callback(this);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
个人理解httphandler是为了创建自定义后缀类型文件按照aspx来解析的一种形式.
以下为收集的一些资料和例子:
ASP.NET 中的内置 HTTP 处理程序
ASP.NET 根据文件扩展名将 HTTP 请求映射到 HTTP 处理程序。每个 HTTP 处理程序都能够处理应用程序中的单个 HTTP URL 或 URL 扩展名组。ASP.NET 包括几种内置的 HTTP 处理程序,如下表所列。
处理程序 | 说明 |
ASP.NET 页处理程序 (*.aspx) | 用于所有 ASP.NET 页的默认 HTTP 处理程序。 |
Web 服务处理程序 (*.asmx) | 用于使用 ASP.NET 创建的 Web 服务页的默认 HTTP 处理程序。 |
ASP.NET 用户控件处理程序 (*.ascx) | 用于所有 ASP.NET 用户控件页的默认 HTTP 处理程序。 |
跟踪处理程序 (trace.axd) | 显示当前页跟踪信息的处理程序。 |
若要创建一个自定义 HTTP 处理程序,可以创建一个可实现 IHttpHandler 接口的类以创建同步处理程序,或者创建一个可实现 IHttpAsyncHandler 的类以创建异步处理程序。两种处理程序接口都要求您实现 IsReusable 属性和 ProcessRequest 方法。IsReusable 属性指定 IHttpHandlerFactory 对象(实际调用适当处理程序的对象)是否可以将您的处理程序放置在池中,并且重新使用它们以提高性能,或是否在每次需要处理程序时都必须创建新实例。ProcessRequest 方法负责实际处理单个 HTTP 请求。
创建文件扩展名
创建一个类文件作为您的 HTTP 处理程序时,可以让您的处理程序响应尚未在 IIS 和 ASP.NET 中映射的任何文件扩展名。例如,如果您在创建用于生成 RSS 源的 HTTP 处理程序,则可以将处理程序映射到扩展名为 .rss的文件。为了让 ASP.NET 知道哪个处理程序将用于您的自定义扩展名文件,必须在 IIS 中将处理程序类文件的扩展名映射到 ASP.NET,并且在您的应用程序中将该扩展名映射到您的自定义处理程序。
默认情况下,ASP.NET 为自定义 HTTP 处理程序映射文件扩展名 .ashx 的方式与将扩展名 .aspx 映射到 ASP.NET 页处理程序的方式相同。因此,如果您创建具有文件扩展名 .ashx 的 HTTP 处理程序类,该处理程序将自动注册到 IIS 和 ASP.NET。
如果想要为您的处理程序创建自定义文件扩展名,则必须显式将该扩展名注册到 IIS 和 ASP.NET。不使用文件扩展名 .ashx 的好处是您的处理程序随后可以重新用于其他扩展名映射。例如,在某个应用程序中,您的自定义处理程序可能响应以 .rss 结尾的请求,而在另一个应用程序中,您的自定义处理程序可能响应以 .feed 结尾的请求。再举一例,您的处理程序可能映射到同一应用程序中的两个文件扩展名,但可能基于扩展名创建两个不同的响应。
异步 HTTP 处理程序
利用异步 HTTP 处理程序可以启动一个外部进程(例如对远程服务器的方法调用),然后继续处理程序的处理工作,而无需等待外部进程结束。在异步 HTTP 处理程序的处理期间,ASP.NET 将通常用于外部进程的线程放回线程池中,直到处理程序收到来自外部进程的回调。这样可以避免阻止线程,并大幅改善了性能,因为一次所能执行的线程数量是有限的。如果许多用户都在请求依赖于外部进程的同步 HTTP 处理程序,那么操作系统可能很快就会用完所有线程,因为大量线程被阻止,正在等待外部进程。
创建异步处理程序时,除了实现 IHttpAsyncHandler 接口,还必须实现 BeginProcessRequest 以启动异步调用来处理单个 HTTP 请求。还必须实现 EndProcessRequest 方法,以便在进程结束时运行清理代码。
自定义 IHttpHandlerFactory 类
IHttpHandlerFactory 类接收请求并负责向相应的 HTTP 处理程序转发请求。您可以通过创建一个实现了 IHttpHandlerFactory 接口的类来创建自定义 HTTP 处理程序工厂。创建自定义处理程序工厂可以更好地控制对 HTTP 请求的处理,因为这样可以基于运行时条件创建不同的处理程序。例如,使用自定义 HTTP 处理程序工厂,可以在 HTTP 请求方法为 PUT 时为某个文件类型实例化一个 HTTP 处理程序,而在该方法为 GET 时实例化另一个 HTTP 处理程序。
以上为一段基础知识.如果要创建一个自定义的扩展名网页,而且要求该网页是收asp.net控的网页必须在iis和asp.net中进行注册.从而自定义扩展名的网页就有了和后缀为.aspx的网页一样的HTTP处理机制.
写个同步调用的例子:
新建一个网站-->在App_code中添加一个HelloWorldHandler类代码如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Data;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Configuration;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web.Security;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web.UI;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web.UI.WebControls;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web.UI.WebControls.WebParts;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web.UI.HtmlControls;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**//// <summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// HelloWorldHandler 的摘要说明
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
/// </summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public class HelloWorldHandler : IHttpHandler
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public HelloWorldHandler()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// TODO: 在此处添加构造函数逻辑
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
IHttpHandler 成员#region IHttpHandler 成员
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public bool IsReusable
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
get ...{ return false; }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void ProcessRequest(HttpContext context)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HttpRequest httpRequest = context.Request;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HttpResponse httpResponse = context.Response;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
httpResponse.Write("<html>");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
httpResponse.Write("<body>");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
httpResponse.Write("<h1>Hello from a synchronous custom HTTP handler.</h1>");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
httpResponse.Write("</body>");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
httpResponse.Write("</html>");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
#endregion
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
在web.config文件中添加如下注册信息:
<system.web>
<httpHandlers>
<add verb="*" path="*.sample" type="HelloWorldHandler"/>
</httpHandlers>
</system.web>
在我们创建的网站中增加一个Test.sample文件.
在iis中配置映射关系:
创建网站-->选择处理程序映射 如下图:
![](http://p.blog.csdn.net/images/p_blog_csdn_net/winnerzone/httphandle1.jpg)
双击进入处理程序映射-->选择添加脚本映射 如下图:
![](http://p.blog.csdn.net/images/p_blog_csdn_net/winnerzone/httphandle2.jpg)
请求路径填写*.sample
可执行文件选择 Microsoft.NET/Framework/v2.0.50727/aspnet_isapi.dll 这个文件.
名称填写.sample
![](http://p.blog.csdn.net/images/p_blog_csdn_net/winnerzone/httphandle3.jpg)
确定.确定.我们打开我们自己创建的Test.sample文件看下?效果如下图:
![](http://p.blog.csdn.net/images/p_blog_csdn_net/winnerzone/httphandle4.jpg)
iis配置与同步调用相同.
web.config中的属性节点的说明
1、verb属性说明了该handle所支持的http请求方式,例如支持post和get方式,verb属性则为"POST,GET";如果支持所有的请求方式,verb属性则用"*"。
2、 path属性说明了对哪些文件的请求才调用该handle来处理,例如你只想在请求my.sample文件时才调用该handle,则path属性为 "my.sample",如果你想所有后缀名为sample的文件(*.possible)都由该handle来处理,则path属性为 "*.sample"。
3、type属性中指定了handle类的命名空间、类名和工程名。ASP.NET runtime会首先到应用程序的bin目录下查找该配件的dll,如果没有找到再到GAC里查找。
因为我使用的是vista所以配置可能和iis6.0不一样,下面是msdn上给的iis6.0的配置说明:
打开“Internet 信息服务(IIS)管理器”。
右击应用程序的名称,然后单击“属性”。
注意 |
有关创建 ASP.NET 应用程序的说明,请参见如何:在 IIS 中创建并配置本地 ASP.NET 网站。 |
在“映射”选项卡上,单击“添加”。
将显示“添加/编辑应用程序扩展名映射”对话框。
在“可执行文件”框中,键入或浏览至 Aspnet_isapi.dll 文件。默认情况下,该文件位于以下位置。
%windows%/Microsoft.NET/Framework/version/
注意 |
可以从其他映射(如 .aspx 文件的映射)获得完整路径和文件名。 |
清除“确认文件是否存在”复选框。
单击“确定”,然后关闭 IIS 管理器。
下面来创建个异步调用的例子.代码如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Data;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Configuration;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web.Security;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web.UI;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web.UI.WebControls;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web.UI.WebControls.WebParts;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web.UI.HtmlControls;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Threading;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
/**//// <summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// HelloWorldAsyncHandler 的摘要说明
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
/// </summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public class HelloWorldAsyncHandler : IHttpAsyncHandler
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public HelloWorldAsyncHandler()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// TODO: 在此处添加构造函数逻辑
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
IHttpAsyncHandler 成员#region IHttpAsyncHandler 成员
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
context.Response.Write("<p>Begin IsThreadPoolThread is " + Thread.CurrentThread.IsThreadPoolThread + "</p> ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
AsynchOperation asynch = new AsynchOperation(cb, context, extraData);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
asynch.StartAsyncWork();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return asynch;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void EndProcessRequest(IAsyncResult result)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
#endregion
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
IHttpHandler 成员#region IHttpHandler 成员
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public bool IsReusable
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
get ...{ return false; }
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void ProcessRequest(HttpContext context)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
throw new InvalidOperationException();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
#endregion
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class AsynchOperation : IAsyncResult
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private bool _completed;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private Object _state;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private AsyncCallback _callback;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private HttpContext _context;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//// <summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 是否成功
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
/// </summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
bool IAsyncResult.IsCompleted ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
get
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return _completed;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
WaitHandle IAsyncResult.AsyncWaitHandle
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
get
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Object IAsyncResult.AsyncState
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
get
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return _state;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bool IAsyncResult.CompletedSynchronously
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
get
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public AsynchOperation(AsyncCallback callback, HttpContext context, Object state)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_callback = callback;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_context = context;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_state = state;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_completed = false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void StartAsyncWork()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ThreadPool.QueueUserWorkItem(new WaitCallback(StartAsyncTask), null);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private void StartAsyncTask(Object workItemState)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_context.Response.Write("<p>Completion IsThreadPoolThread is " + Thread.CurrentThread.IsThreadPoolThread + "</p> ");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_context.Response.Write("Hello World from Async Handler!");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_completed = true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
_callback(this);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
相关文章推荐
- Httphandler学习中的知识点.
- Httphandler,HttpModule学习笔记
- ASP.NET基础之HttpHandler学习
- web.config 学习之httpHandler
- Socket、Tcp、Udp、Http 知识点学习
- ASP.NET 学习笔记_06 Httphandler
- Httphandle Httpmodule HttpHandler Factory 学习整理
- 学习笔记---httpHandler处理程序
- ASP.NET 中Http处理流程与 HttpModule,HttpHandler学习之初步认知
- Android之okHttpClient+handler+LruCache缓存网络图片学习笔记(通用MVP模式)
- Go圣经-学习笔记之http.Handler接口
- HttpModule和HttpHandler学习
- ASP.NET 中Http处理流程与 HttpModule,HttpHandler学习之初步认知
- ASP.NET基础之HttpHandler学习
- (转)ASP.NET基础之HttpHandler学习
- HTTP学习2-HttpHandler 概述
- HttpModule HttpHandler HttpHandlerFactory 学习笔记
- ASP.NET基础之HttpHandler学习
- ASP.NET基础之HttpHandler学习
- HttpHandler学习