ASP.NETSession实现会话的建立流程
点击次数:20 次 发布日期:2008-11-27 02:21:22 作者:源代码网
|
源代码网推荐 HTTP 协议之所以能够获得如此大的成功,其设计实现的简洁性和无状态连接的高效率是很重要的原因。而为了在无状态的 HTTP 请求和有状态的客户端操作之间达到平衡,产生了服务器端会话 (Session) 的概念。客户端在连接到服务器后,就由 Web 服务器产生并维护一个客户端的会话;当客户端通过无状态 HTTP 协议再次连接到服务器时,服务器根据客户端提交的某种凭据,如 Cookie 或 URL 参数,将客户关联到某个会话上。这种思路在各种开发语言和开发环境中大量得到应用。 源代码网推荐 在 ASP.NET 中,Web 应用程序和会话状态被分别进行维护,通过 HttpApplication 和 HttpSessionState 分离 Web 应用程序与会话的功能。应用程序层逻辑在 Global.asax 文件中实现,运行时编译成 System.Web.HttpApplication 的实例;会话则作为单独的 System.Web.SessionState.HttpSessionState 实例,由服务器统一为每个用户会话维护,通过 ASP.NET 页面编译成的 System.Web.UI.Page 对象子类的 Session 属性访问。关于 ASP.NET 中不同层次关系可参考我以前的一篇文章《.NET 1.1中预编译ASP.NET页面实现原理浅析 [1] 自动预编译机制浅析》,以下简称【文1】。 源代码网推荐 源代码网推荐 ASP.NET 在处理客户端请求时,首先将根据客户端环境,生成一个 System.Web.HttpContext 对象,并将此对象作为执行上下文传递给后面的页面执行代码。 源代码网推荐 在【文1】的分析中我们可以看到,HttpRuntime 在处理页面请求之前,根据 HttpWorkerRequest 中给出的环境,构造 HttpContext 对象,并以次对象作为参数从应用程序池中获取可用应用程序。简要代码如下: 源代码网推荐 以下内容为程序代码: 源代码网推荐 源代码网推荐 private void HttpRuntime.ProcessRequestInternal(HttpWorkerRequest wr) 源代码网推荐 { 源代码网推荐 // 构造 HTTP 调用上下文对象 源代码网推荐 HttpContext ctxt = new HttpContext(wr, 0); 源代码网推荐 源代码网推荐 //... 源代码网推荐 源代码网推荐 // 获取当前 Web 应用程序实例 源代码网推荐 IHttpHandler handler = HttpApplicationFactory.GetApplicationInstance(ctxt); 源代码网推荐 源代码网推荐 // 调用 handler 实际处理页面请求 源代码网推荐 } 源代码网推荐 源代码网推荐 HttpApplicationFactory 工厂内部维护了一个可用的应用程序实例缓冲池,用户降低应用程序对象构造的负荷。 源代码网推荐 如果池中没有可用的应用程序对象实例,此对象工厂最终会调用 System.Web.HttpRuntime.CreateNonPublicInstance 方法构造新的应用程序实例,并调用其 InitInternal 方法初始化。详细步骤分析见【文1】 源代码网推荐 以下内容为程序代码: 源代码网推荐 源代码网推荐 internal static IHttpHandler HttpApplicationFactory.GetApplicationInstance(HttpContext ctxt) 源代码网推荐 { 源代码网推荐 // 处理定制应用程序 源代码网推荐 //... 源代码网推荐 源代码网推荐 // 处理调试请求 源代码网推荐 //... 源代码网推荐 源代码网推荐 // 判断是否需要初始化当前 HttpApplicationFactory 实例 源代码网推荐 //... 源代码网推荐 源代码网推荐 // 获取 Web 应用程序实例 源代码网推荐 return HttpApplicationFactory._theApplicationFactory.GetNormalApplicationInstance(ctxt); 源代码网推荐 } 源代码网推荐 源代码网推荐 private HttpApplication HttpApplicationFactory.GetNormalApplicationInstance(HttpContext context) 源代码网推荐 { 源代码网推荐 HttpApplication app = null; 源代码网推荐 源代码网推荐 // 尝试从已施放的 Web 应用程序实例队列中获取 源代码网推荐 //... 源代码网推荐 源代码网推荐 if(app == null) 源代码网推荐 { 源代码网推荐 // 构造新的 Web 应用程序实例 源代码网推荐 app = (HttpApplication)System.Web.HttpRuntime.CreateNonPublicInstance(this._theApplicationType); 源代码网推荐 源代码网推荐 // 初始化 Web 应用程序实例 源代码网推荐 app.InitInternal(context, this._state, this._eventHandlerMethods); 源代码网推荐 } 源代码网推荐 源代码网推荐 return app; 源代码网推荐 } 源代码网推荐 源代码网推荐 这里的 System.Web.HttpApplication.InitInternal 函数完成对应用程序对象的初始化工作,包括调用 HttpApplication.InitModules 函数初始化 HTTP 模块(后面将详细介绍),并将作为参数传入的 HttpContext 实例保存到 HttpApplication._context 字段中。而此 HTTP 上下文对象将被后面用于获取会话对象。 源代码网推荐 以下内容为程序代码: 源代码网推荐 源代码网推荐 public class HttpApplication : ... 源代码网推荐 { 源代码网推荐 private HttpContext _context; 源代码网推荐 private HttpSessionState _session; 源代码网推荐 源代码网推荐 public HttpSessionState Session 源代码网推荐 { 源代码网推荐 get 源代码网推荐 { 源代码网推荐 HttpSessionState state = null; 源代码网推荐 if (this._session != null) 源代码网推荐 { 源代码网推荐 state = this._session; 源代码网推荐 } 源代码网推荐 else if (this._context != null) 源代码网推荐 { 源代码网推荐 state = this._context.Session; 源代码网推荐 } 源代码网推荐 if (state == null) 源代码网推荐 { 源代码网推荐 Throw new HttpException(HttpRuntime.FormatResourceString("Session_not_available"[img]/images/wink.gif[/img]); 源代码网推荐 } 源代码网推荐 return state; 源代码网推荐 } 源代码网推荐 } 源代码网推荐 } 源代码网推荐 源代码网推荐 而在 ASP.NET 页面中获取会话的方法也是类似,都是通过 HttpContext 来完成的。 源代码网推荐 以下内容为程序代码: 源代码网推荐 源代码网推荐 public class Page : ... 源代码网推荐 { 源代码网推荐 private HttpSessionState _session; 源代码网推荐 private bool _sessionRetrieved; 源代码网推荐 源代码网推荐 public virtual HttpSessionState Session 源代码网推荐 { 源代码网推荐 get 源代码网推荐 { 源代码网推荐 if (!this._sessionRetrieved) 源代码网推荐 { 源代码网推荐 this._sessionRetrieved = true; 源代码网推荐 try 源代码网推荐 { 源代码网推荐 this._session = this.Context.Session; 源代码网推荐 } 源代码网推荐 catch (Exception) 源代码网推荐 { 源代码网推荐 } 源代码网推荐 } 源代码网推荐 if (this._session == null) 源代码网推荐 { 源代码网推荐 Throw new HttpException(HttpRuntime.FormatResourceString("Session_not_enabled"[img]/images/wink.gif[/img]); 源代码网推荐 } 源代码网推荐 return this._session; 源代码网推荐 } 源代码网推荐 } 源代码网推荐 } 源代码网推荐 源代码网推荐 在 HttpContext 中,实际上是通过一个哈希表保存诸如会话对象之类信息的 源代码网推荐 以下内容为程序代码: 源代码网推荐 源代码网推荐 public sealed class HttpContext : ... 源代码网推荐 { 源代码网推荐 private Hashtable _items; 源代码网推荐 源代码网推荐 public IDictionary Items 源代码网推荐 { 源代码网推荐 get 源代码网推荐 { 源代码网推荐 if (this._items == null) 源代码网推荐 { 源代码网推荐 this._items = new Hashtable(); 源代码网推荐 } 源代码网推荐 return this._items; 源代码网推荐 } 源代码网推荐 } 源代码网推荐 源代码网推荐 public HttpSessionState Session 源代码网推荐 { 源代码网推荐 get 源代码网推荐 { 源代码网推荐 return ((HttpSessionState) this.Items["AspSession"]); 源代码网推荐 } 源代码网推荐 } 源代码网推荐 } 源代码网推荐 源代码网推荐 而 HttpContext.Session 所访问的又是哪儿来的呢?这就又需要回到我们前面提及的 HttpApplication.InitModules 函数。 源代码网推荐 源代码网推荐 在 .NET 安装目录 Config 子目录下的 machine.config 定义了全局性的配置信息,而 HttpApplication 就是使用其中 system.web 一节的配置信息进行初始化的。 源代码网推荐 以下内容为程序代码: 源代码网推荐 源代码网推荐 源代码网推荐 源代码网推荐 源代码网推荐 源代码网推荐 源代码网推荐 源代码网推荐 源代码网推荐 源代码网推荐 源代码网推荐 源代码网推荐 源代码网推荐 源代码网推荐 源代码网推荐 httpModules 节点指定了 HttpApplication 需要初始化的模块列表,而在前面提到的 HttpApplication.InitModules 函数正式根据此列表进行初始化的 源代码网推荐 以下内容为程序代码: 源代码网推荐 源代码网推荐 private void HttpApplication.InitModules() 源代码网推荐 { 源代码网推荐 HttpModulesConfiguration cfgModules = ((HttpModulesConfiguration) HttpContext.GetAppConfig("system.web/httpModules"[img]/images/wink.gif[/img]); 源代码网推荐 源代码网推荐 if (cfgModules == null) 源代码网推荐 { 源代码网推荐 Throw new HttpException(HttpRuntime.FormatResourceString("Missing_modules_config"[img]/images/wink.gif[/img]); 源代码网推荐 } 源代码网推荐 _moduleCollection = cfgModules.CreateModules(); 源代码网推荐 源代码网推荐 for(int i = 0; i < _moduleCollection.Count; i++) 源代码网推荐 { 源代码网推荐 _moduleCollection[i].Init(this); 源代码网推荐 } 源代码网推荐 源代码网推荐 GlobalizationConfig cfgGlobal = ((GlobalizationConfig) HttpContext.GetAppConfig("system.web/globalization"[img]/images/wink.gif[/img]); 源代码网推荐 if (cfgGlobal != null) 源代码网推荐 { 源代码网推荐 _appLevelCulture = cfgGlobal.Culture; 源代码网推荐 _appLevelUICulture = cfgGlobal.UICulture; 源代码网推荐 } 源代码网推荐 } 源代码网推荐 源代码网推荐 Session 节点对于的 System.Web.SessionState.SessionStateModule 对象将被 HttpModulesConfiguration.CreateModules 方法构造,并调用其 Init 函数初始化。SessionStateModule 类实际上就是负责管理并创建会话,用户完全可以自行创建一个实现 IHttpModule 接口的类,实现会话的控制,如实现支持集群的状态同步等等。 源代码网推荐 SessionStateModule.Init 方法主要负责 machine.config 文件中的 ses 源代码网推荐 源代码网推荐 源代码网推荐 源代码网供稿. |
