最近在看生命周期的东西 有一点地方不明白 还请了解的朋友帮忙指导一下 多谢
写的比较乱 不知道我描述的你们看不看的懂 下面标红的字体是我的疑问
HttpRuntime请求返回HttpApplication得时候
IHttpHandler app = HttpApplicationFactory.GetApplicationInstance(context);
internal static IHttpHandler GetApplicationInstance(HttpContext context) { if (_customApplication != null) return _customApplication; // Check to see if it's a debug auto-attach request if (context.Request.IsDebuggingRequest) return new HttpDebugHandler(); _theApplicationFactory.EnsureInited(); _theApplicationFactory.EnsureAppStartCalled(context); return _theApplicationFactory.GetNormalApplicationInstance(context); }
_theApplicationFactory.EnsureAppStartCalled(context); 在调用FireApplicationOnStart方法里 创建了一个HttpApplication GetSpecialApplicationInstance
internal void InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) { // Remember state _state = state; try { // Remember the context for the initialization if (context != null) { _initContext = context; _initContext.ApplicationInstance = this; } // if we're doing integrated pipeline wireup, then appContext is non-null and we need to init modules and register event subscriptions with IIS if (appContext != IntPtr.Zero) { // 1694356: app_offline.htm and <httpRuntime enabled=/> require that we make this check here for integrated mode using (new ApplicationImpersonationContext()) { HttpRuntime.CheckApplicationEnabled(); } // retrieve app level culture InitAppLevelCulture(); Debug.Trace("PipelineRuntime", "InitSpecial for " + appContext.ToString() + "\n"); RegisterEventSubscriptionsWithIIS(appContext, context, handlers); } else { // retrieve app level culture InitAppLevelCulture(); // Hookup event handlers via reflection if (handlers != null) { HookupEventHandlersForApplicationAndModules(handlers); } } // if we're doing integrated pipeline wireup, then appContext is non-null and we need to register the application (global.asax) event handlers if (appContext != IntPtr.Zero) { if (_appPostNotifications != 0 || _appRequestNotifications != 0) { RegisterIntegratedEvent(appContext, HttpApplicationFactory.applicationFileName, _appRequestNotifications, _appPostNotifications, this.GetType().FullName, MANAGED_PRECONDITION, false); } } } finally { _initSpecialCompleted = true; // Do not hold on to the context if (_initContext != null) { _initContext.ApplicationInstance = null; _initContext = null; } } }
在 app.InitSpecial(_state, _eventHandlerMethods, appContext, context);在这个方法里
集成模式下 RegisterEventSubscriptionsWithIIS 调用了HookupEventHandlersForApplicationAndModules 然后再调用各个httpModule的init方法
HookupEventHandlersForApplicationAndModules代码在下面
在调用HookupEventHandlersForApplicationAndModules这里 有个疑问 获取完addMethod = reflectionEvent.GetAddMethod();之后 addMethod.Invoke(target, new Object[1]{handlerDelegate});这里是httpmodule里+=的执行吗? 执行这个addMethod函数是干啥的?
经典模式下 只执行了HookupEventHandlersForApplicationAndModules(handlers);
之后再触发application_start之后HttpApplication被回收 回到 return _theApplicationFactory.GetNormalApplicationInstance(context);
又取了一个新的HttpApplication 再InitInternal方法里 集成经典模式 都调用了InitModulesCommon();调用各个httpModule的init方法
对于集成模式下 httpModule的init方法 调用了两次 创建两次HttpApplication调用两次httpModule的init 这样做为啥
private void HookupEventHandlersForApplicationAndModules(MethodInfo[] handlers) { _currentModuleCollectionKey = HttpApplicationFactory.applicationFileName; if(null == _pipelineEventMasks) { Dictionary<string, RequestNotification> dict = new Dictionary<string, RequestNotification>(); BuildEventMaskDictionary(dict); if(null == _pipelineEventMasks) { _pipelineEventMasks = dict; } } for (int i = 0; i < handlers.Length; i++) { MethodInfo appMethod = handlers[i]; String appMethodName = appMethod.Name; int namePosIndex = appMethodName.IndexOf('_'); String targetName = appMethodName.Substring(0, namePosIndex); // Find target for method Object target = null; if (StringUtil.EqualsIgnoreCase(targetName, "Application")) target = this; else if (_moduleCollection != null) target = _moduleCollection[targetName]; if (target == null) continue; // Find event on the module type Type targetType = target.GetType(); EventDescriptorCollection events = TypeDescriptor.GetEvents(targetType); string eventName = appMethodName.Substring(namePosIndex+1); EventDescriptor foundEvent = events.Find(eventName, true); if (foundEvent == null && StringUtil.EqualsIgnoreCase(eventName.Substring(0, 2), "on")) { eventName = eventName.Substring(2); foundEvent = events.Find(eventName, true); } MethodInfo addMethod = null; if (foundEvent != null) { EventInfo reflectionEvent = targetType.GetEvent(foundEvent.Name); Debug.Assert(reflectionEvent != null); if (reflectionEvent != null) { addMethod = reflectionEvent.GetAddMethod(); } } if (addMethod == null) continue; ParameterInfo[] addMethodParams = addMethod.GetParameters(); if (addMethodParams.Length != 1) continue; // Create the delegate from app method to pass to AddXXX(handler) method Delegate handlerDelegate = null; ParameterInfo[] appMethodParams = appMethod.GetParameters(); if (appMethodParams.Length == 0) { // If the app method doesn't have arguments -- // -- hookup via intermidiate handler // only can do it for EventHandler, not strongly typed if (addMethodParams[0].ParameterType != typeof(System.EventHandler)) continue; ArglessEventHandlerProxy proxy = new ArglessEventHandlerProxy(this, appMethod); handlerDelegate = proxy.Handler; } else { // Hookup directly to the app methods hoping all types match try { handlerDelegate = Delegate.CreateDelegate(addMethodParams[0].ParameterType, this, appMethodName); } catch { // some type mismatch continue; } } // Call the AddXXX() to hook up the delegate try { addMethod.Invoke(target, new Object[1]{handlerDelegate}); } catch { if (HttpRuntime.UseIntegratedPipeline) { throw; } } if (eventName != null) { if (_pipelineEventMasks.ContainsKey(eventName)) { if (!StringUtil.StringStartsWith(eventName, "Post")) { _appRequestNotifications |= _pipelineEventMasks[eventName]; } else { _appPostNotifications |= _pipelineEventMasks[eventName]; } } } } }