ASP.NET分析(一)--运行机制

 
ASP.NET的运行机制
1.   相关资源:
MSDN中关于ASP.NET应用程序生周期
ms-help://MS.MSDNQTR.v80.chs/MS.MSDN.v80/MS.VisualStudio.v80.chs/dv_aspnetcon/html/de0d8a1c-b1bc-48e1-b246-26e32289a82f.htm
 
2.   建立相关对象的过程
(1) 第一次请求时创建HttpRuntime对象:
 
(2) 为每个请求创建对象(HttpContext)
(3) 为每个请求创建HttpApplication对象:
3.   相关对象
1.       HttpRuntime
2.       ApplicationManager
3.       HostingEnvironment
4.   通过ashx分析
通过ashx可以看出ASP.NET的运行机制。
ashx与Java的Servlet类似,是原生态的处理http请求和输出的对象。
1.1.分析ASP.NET 2.0的默认配置
(1) 配置
<httpHandlers>
     <add path="trace.axd" verb="*" type="System.Web.Handlers.TraceHandler" validate="True" />
     <add path="WebResource.axd" verb="GET" type="System.Web.Handlers.AssemblyResourceLoader"
          validate="True" />
     <add path="*.axd" verb="*" type="System.Web.HttpNotFoundHandler" validate="True" />
     <add path="*.aspx" verb="*" type="System.Web.UI.PageHandlerFactory" validate="True" />
     <add path="*.ashx" verb="*" type="System.Web.UI.SimpleHandlerFactory" validate="True" />
     <add path="*.asmx" verb="*" type="System.Web.Services.Protocols.WebServiceHandlerFactory,
System.Web.Services, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" validate="False" />
     <add path="*.rem" verb="*" type="System.Runtime.Remoting.Channels.Http.HttpRemotingHandlerFactory,
System.Runtime.Remoting, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
          validate="False" />
     <add path="*.soap" verb="*" type="System.Runtime.Remoting.Channels.Http.HttpRemotingHandlerFactory,
System.Runtime.Remoting, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
          validate="False" />
     <add path="*.asax" verb="*" type="System.Web.HttpForbiddenHandler" validate="True" />
     <add path="*.ascx" verb="*" type="System.Web.HttpForbiddenHandler" validate="True" />
     <add path="*.master" verb="*" type="System.Web.HttpForbiddenHandler" validate="True" />
     <add path="*.skin" verb="*" type="System.Web.HttpForbiddenHandler" validate="True" />
     <add path="*.browser" verb="*" type="System.Web.HttpForbiddenHandler" validate="True" />
     <add path="*.sitemap" verb="*" type="System.Web.HttpForbiddenHandler" validate="True" />
     <add path="*.dll.config" verb="GET,HEAD" type="System.Web.StaticFileHandler" validate="True" />
     <add path="*.exe.config" verb="GET,HEAD" type="System.Web.StaticFileHandler" validate="True" />
     <add path="*.config" verb="*" type="System.Web.HttpForbiddenHandler" validate="True" />
     <add path="*.cs" verb="*" type="System.Web.HttpForbiddenHandler" validate="True" />
     <add path="*.csproj" verb="*" type="System.Web.HttpForbiddenHandler" validate="True" />
     <add path="*.vb" verb="*" type="System.Web.HttpForbiddenHandler" validate="True" />
     <add path="*.vbproj" verb="*" type="System.Web.HttpForbiddenHandler" validate="True" />
     <add path="*.webinfo" verb="*" type="System.Web.HttpForbiddenHandler" validate="True" />
     <add path="*.licx" verb="*" type="System.Web.HttpForbiddenHandler" validate="True" />
     <add path="*.resx" verb="*" type="System.Web.HttpForbiddenHandler" validate="True" />
     <add path="*.resources" verb="*" type="System.Web.HttpForbiddenHandler" validate="True" />
     <add path="*.mdb" verb="*" type="System.Web.HttpForbiddenHandler" validate="True" />
     <add path="*.vjsproj" verb="*" type="System.Web.HttpForbiddenHandler" validate="True" />
     <add path="*.java" verb="*" type="System.Web.HttpForbiddenHandler" validate="True" />
     <add path="*.jsl" verb="*" type="System.Web.HttpForbiddenHandler" validate="True" />
     <add path="*.ldb" verb="*" type="System.Web.HttpForbiddenHandler" validate="True" />
     <add path="*.dsdgm" verb="*" type="System.Web.HttpForbiddenHandler" validate="True" />
     <add path="*.ssdgm" verb="*" type="System.Web.HttpForbiddenHandler" validate="True" />
     <add path="*.lsad" verb="*" type="System.Web.HttpForbiddenHandler" validate="True" />
     <add path="*.ssmap" verb="*" type="System.Web.HttpForbiddenHandler" validate="True" />
     <add path="*.cd" verb="*" type="System.Web.HttpForbiddenHandler" validate="True" />
     <add path="*.dsprototype" verb="*" type="System.Web.HttpForbiddenHandler" validate="True" />
     <add path="*.lsaprototype" verb="*" type="System.Web.HttpForbiddenHandler" validate="True" />
     <add path="*.sdm" verb="*" type="System.Web.HttpForbiddenHandler" validate="True" />
     <add path="*.sdmDocument" verb="*" type="System.Web.HttpForbiddenHandler" validate="True" />
     <add path="*.mdf" verb="*" type="System.Web.HttpForbiddenHandler" validate="True" />
     <add path="*.ldf" verb="*" type="System.Web.HttpForbiddenHandler" validate="True" />
     <add path="*" verb="GET,HEAD,POST" type="System.Web.DefaultHttpHandler" validate="True" />
     <add path="*" verb="*" type="System.Web.HttpMethodNotAllowedHandler" validate="True" />
</httpHandlers>
(2) 分析
观察.NET FrameWork 2.0的默认配置,我们发现,其中有如下几个类是用来处理不同类型的文件的:
后缀
说明
.aspx
System.Web.UI.PageHandlerFactory
Asp.net的处理程序
.ashx
System.Web.UI.SimpleHandlerFactory
http请求的处理程序
其它
System.Web.HttpForbiddenHandler
禁止请求的处理程序
 
System.Web.StaticFileHandler
 
*
System.Web.DefaultHttpHandler
 
trace.axd
System.Web.Handlers.TraceHandler
跟踪程序
WebResource.axd
System.Web.Handlers.AssemblyResourceLoader
 
.rem
System.Runtime.Remoting.Channels.Http.HttpRemotingHandlerFactory
.NET Remoting处理程序
我们发现,所有的配置文件中,都实现了IHttpHandlerFactory接口,分析这个接口,可以看出,这是一个Factory,这个Factory通过不同的配置建立不同的处理http的对象。
       IHttpHandlerFactory的结构:
 IHttpHandler GetHandler()
返回实现 IHttpHandler 接口的类的实例。 
 ReleaseHandler
使工厂可以重用现有的处理程序实例。 
 (3) 结论
1.ApplicationManager.CreateObject() 创建HostingEnvironment
2.HostingEnvironement
3.HttpRuntime.ProcessRequest()
private void ProcessRequestInternal(HttpWorkerRequest wr)
{
      HttpContext context1 = new HttpContext(wr, false);
      context1.Response.InitResponseWriter();
      IHttpHandler handler1 = HttpApplicationFactory.GetApplicationInstance(context1);
      if (handler1 is IHttpAsyncHandler)
      {
             IHttpAsyncHandler handler2 = (IHttpAsyncHandler) handler1;
             context1.AsyncAppHandler = handler2;
             handler2.BeginProcessRequest(context1, this._handlerCompletionCallback, context1);
       }
       else
       {
             handler1.ProcessRequest(context1);
             this.FinishRequest(context1.WorkerRequest, context1, null);
       }
      }
      catch (Exception exception1)
      {
            context1.Response.InitResponseWriter();
            this.FinishRequest(wr, context1, exception1);
      }
}
4.各种页面处理做各种类型的ProcessRequest。
aspx如下:
private void ProcessRequestMain(bool includeStagesBeforeAsyncPoint, bool includeStagesAfterAsyncPoint)
{
     
}
其序列图如下所示:
而ashx就是用户实现了IHttpHander接口。
 
发布了13 篇原创文章 · 获赞 0 · 访问量 2万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览