HYXT Blog

we produce valuable software for K12.
clock November 12, 2008 13:25 by author Sky Jia (贾超)

Posted by Al Tenhundfeld on Oct 26, 2008 11:49 PM

 With the recent release of URL-rewrite module for IIS 7.0 and the inclusion of ASP.NET routing into the .NET Framework 3.5 SP1, many ASP.NET developers are questioning how these two features relate to each other and when each should be used.

Ruslan Yakushev has an instructive post on LearnIIS.net.

The essential difference is that IIS URL rewriting is handled at a lower level than ASP.NET routing and is invisible to the client.

Ruslan provides a visual workflow of the IIS 7 URL Rewriting process. You'll see that the URL Rewrite Module is activated before the request is passed to a request handler, e.g., the ASP.NET managed ASPX handler. IIS URL rewriting is unaware of specific request handlers.

He also provides a visual workflow the ASP.NET Routing process. You'll see the ASP.NET routing is a request dispatcher and must be fully aware of which handler a specific request should be routed to.

From Ruslan's description:

  • 1. URL rewriting is used to manipulate URL paths before the request is handled by the Web server. The URL-rewriting module does not know anything about what handler will eventually process the rewritten URL. In addition, the actual request handler might not know that the URL has been rewritten.
  • 2. ASP.NET routing is used to dispatch a request to a handler based on the requested URL path. As opposed to URL rewriting, the routing component knows about handlers and selects the handler that should generate a response for the requested URL. You can think of ASP.NET routing as an advanced handler-mapping mechanism.

 

  • The IIS URL-rewrite module can be used with any type of Web application, which includes ASP.NET, PHP, ASP, and static files. ASP.NET routing can be used only with .NET Framework-based Web applications.
  • The IIS URL-rewrite module works the same way regardless of whether integrated or classic IIS pipeline mode is used for the application pool. For ASP.NET routing, it is preferable to use integrated pipeline mode. ASP.NET routing can work in classic mode, but in that case the application URLs must include file extensions or the application must be configured to use "*" handler mapping in IIS.
  • The URL-rewrite module can make rewriting decisions based on domain names, HTTP headers, and server variables. By default, ASP.NET routing works only with URL paths and with the HTTP-Method header.
  • In addition to rewriting, the URL-rewrite module can perform HTTP redirection, issue custom status codes, and abort requests. ASP.NET routing does not perform those tasks.
  • The URL-rewrite module is not extensible in its current version. ASP.NET routing is fully extensible and customizable. 

clock October 13, 2008 10:46 by author Sky Jia (贾超)

ASP.NET是一个非常强大的构建Web应用的平台,它提供了极大的灵活性和能力以致于可以用它来构建所有类型的Web应用。 
绝大多数的人只熟悉高层的框架如: WebForms WebServices --这些都在ASP.NET层次结构在最高层。

这篇文章的资料收集整理自各种微软公开的文档,通过比较 IIS5IIS6IIS7 这三代 IIS 对请求的处理过程,让我们熟悉 ASP.NET的底层机制并对请求(request)是怎么从Web服务器传送到ASP.NET运行时有所了解。通过对底层机制的了解,可以让我们对 ASP.net 有更深的理解。

IIS 5 ASP.net 请求处理过程

对图的解释:

IIS 5.x 一个显著的特征就是 Web Server 和真正的 ASP.NET Application 的分离。作为 Web Server IIS运行在一个名为 InetInfo.exe 的进程上,InetInfo.exe 是一个Native Executive,并不是一个托管的程序,而我们真正的 ASP.NET Application 则是运行在一个叫做 aspnet_wp Worker Process 上面,在该进程初始化的时候会加载CLR,所以这是一个托管的环境。

ISAPI  指能够处理各种后缀名的应用程序。 ISAPI 是下面单词的简写Internet Server Application Programe Interface,互联网服务器应用程序接口。

IIS 5 模式的特点:

1、首先,同一台主机上在同一时间只能运行一个 aspnet_wp 进程,每个基于虚拟目录的 ASP.NET Application 对应一个 Application Domain ,也就是说每个 Application 都运行在同一个 Worker Process 中,Application之间的隔离是基于 Application Domain 的,而不是基于Process的。

2、其次,ASP.NET  ISAPI 不但负责创建 aspnet_wp Worker Process,而且负责监控该进程,如果检测到 aspnet_wp Performance 降低到某个设定的下限,ASP.NET  ISAPI 会负责结束掉该进程。当 aspnet_wp 结束掉之后,后续的 Request 会导致ASP.NET ISAPI 重新创建新的 aspnet_wp Worker Process

3、最后,由于 IIS Application 运行在他们各自的进程中,他们之间的通信必须采用特定的通信机制。本质上 IIS 所在的 InetInfo 进程和 Worker Process 之间的通信是同一台机器不同进程的通信(local interprocess communications),处于Performance的考虑,他们之间采用基于Named pipe的通信机制。ASP.NET ISAPIWorker Process之间的通信通过他们之间的一组Pipe实现。同样处于Performance的原因,ASP.NET ISAPI 通过异步的方式将Request 传到Worker Process 并获得 Response,但是 Worker Process 则是通过同步的方式向 ASP.NET ISAPI 获得一些基于 Server 的变量。

  

IIS6 ASP.net 请求处理过程

对图的解释:

IIS 5.x 是通过 InetInfo.exe 监听 Request 并把Request分发到Work Process。换句话说,在IIS 5.x中对Request的监听和分发是在User Mode中进行,在IIS 6中,这种工作被移植到kernel Mode中进行,所有的这一切都是通过一个新的组件:http.sys 来负责。

注:为了避免用户应用程序访问或者修改关键的操作系统数据,windows提供了两种处理器访问模式:用户模式(User Mode)和内核模式(Kernel Mode)。一般地,用户程序运行在User mode下,而操作系统代码运行在Kernel Mode下。Kernel Mode的代码允许访问所有系统内存和所有CPU指令。

User Mode下,http.sys接收到一个基于 aspx http request,然后它会根据IIS中的 Metabase 查看该基于该 Request Application 属于哪个Application Pool如果该Application Pool不存在,则创建之。否则直接将 request 发到对应Application Pool Queue中。

每个 Application Pool 对应着一个Worker Processw3wp.exe,毫无疑问他是运行在User Mode下的。在IIS Metabase 中维护着 Application Pool worker processMappingWASWeb Administrative service)根据这样一个mapping,将存在于某个Application Pool Queuerequest 传递到对应的worker process(如果没有,就创建这样一个进程)。在 worker process 初始化的时候,加载ASP.NET ISAPIASP.NET ISAPI 进而加载CLR。最后的流程就和IIS 5.x一样了:通过AppManagerAppDomainFactory Create方法为 Application 创建一个Application Domain;通过 ISAPIRuntime ProcessRequest处理Request,进而将流程进入到ASP.NET Http Runtime Pipeline

  

IIS 7  ASP.net 请求处理过程

  

IIS7 站点启动并处理请求的步骤如下图:

步骤 1 6 ,是处理应用启动,启动好后,以后就不需要再走这个步骤了。

上图的8个步骤分别如下:

1、当客户端浏览器开始HTTP 请求一个WEB 服务器的资源时,HTTP.sys 拦截到这个请求。 
2
HTTP.sys contacts WAS to obtain information from the configuration store.

3WAS 向配置存储中心请求配置信息。applicationHost.config 
4
WWW 服务接受到配置信息,配置信息指类似应用程序池配置信息,站点配置信息等等。 
5
WWW 服务使用配置信息去配置 HTTP.sys 处理策略。 
6
WAS starts a worker process for the application pool to which the request was made.

7The worker process processes the request and returns a response to HTTP.sys.

8、客户端接受到处理结果信息。

W3WP.exe 进程中又是如果处理得呢?? IIS 7 的应用程序池的托管管道模式分两种:经典和集成。这两种模式下处理策略各不相通。

本文作者:郭红俊 http://blog.joycode.com/ghj

  

IIS 6 以及 IIS7 经典模式的托管管道的架构

       IIS7之前,ASP.NET 是以 IIS ISAPI extension 的方式外加到 IIS,其实包括 ASP 以及 PHP,也都以相同的方式配置(PHP IIS 采用了两种配置方式,除了 IIS ISAPI extension 的方式,也包括了 CGI 的方式,系统管理者能选择 PHP 程序的执行方式),因此客户端对 IIS HTTP 请求会先经由 IIS 处理,然后 IIS 根据要求的内容类型,如果是 HTML 静态网页就由 IIS 自行处理,如果不是,就根据要求的内容类型,分派给各自的 IIS ISAPI extension;如果要求的内容类型是 ASP.NET,就分派给负责处理 ASP.NET IIS ISAPI extension,也就是 aspnet_isapi.dll。下图是这个架构的示意图。

IIS  7 应用程序池的托管管道模式  经典  模式也是这样的工作原理。这种模式是兼容IIS 6 的方式,以减少升级的成本。

IIS6 的执行架构图,以及 IIS7  应用程序池配置成经典模式的执行架构图

  

IIS  7 应用程序池的托管管道模式  集成模式

       IIS 7 完全整合 .NET 之后,架构的处理顺序有了很大的不同(如下图),最主要的原因就是 ASP.NET IIS 插件(ISAPI extension)的角色,进入了 IIS 核心,而且也能以 ASP.NET 模块负责处理 IIS 7 的诸多类型要求。这些 ASP.NET 模块不只能处理 ASP.NET 网页程序,也能处理其他如 ASP 程序、PHP 程序或静态 HTML 网页,也因为 ASP.NET 的诸多功能已经成为 IIS 7 的一部份,因此 ASP 程序、PHP 程序或静态 HTML 网页等类型的要求,也能使用像是Forms认证(Forms Authentication)或输出缓存(Output Cache)等 ASP.NET 2.0 的功能(但须修改 IIS 7 的设定值)。也因为 IIS 7 允许自行以 ASP.NET API 开发并加入模块,因此 ASP.NET 网页开发人员将更容易扩充 IIS 7 和网站应用程序的功能,甚至能自行以 .NET 编写管理 IIS 7 的程序(例如以程控 IIS 7 以建置网站或虚拟目录)。

IIS 7 的执行架构图(集成托管信道模式下的架构)

  

小结

IIS5 IIS6 的改进,主要是 HTTP.sys 的改进。

IIS6 IIS7 的改进,主要是 ISAPI 的改进。

  

参考资料:

ASP.NET Process Model之一:IIS ASP.NET ISAPI 
http://www.cnblogs.com/artech/archive/2007/09/09/887528.html

ASP.NET Internals – IIS and the Process Model 
http://dotnetslackers.com/articles/iis/ASPNETInternalsIISAndTheProcessModel.aspx

模组化的IIS 7 .NET 能力整合 
http://www.microsoft.com/taiwan/technet/columns/profwin/33-iis7-componentization-integration.mspx

Introduction to IIS 7.0 Architecture 
http://learn.iis.net/page.aspx/101/introduction-to-iis7-architecture/


Search

Calendar

<<  September 2010  >>
SuMoTuWeThFrSa
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789

Categories

Tags