MVC源码解析 - Http Pipeline 解析(上)

IHttpHandler applicationInstance = HttpApplicationFactory.GetApplicationInstance(context);

上一篇说到了创建 HttpApplication 对象出来(就是上面这句话了), 那么在创建对象的时候, 做了些什么事情呢, 是不是只是创建了对象而已呢, 继续分析.

internal static IHttpHandler GetApplicationInstance(HttpContext context)
{
    if (_customApplication != null)
    {
        return _customApplication;
    }
    if (context.Request.IsDebuggingRequest)
    {
        return new HttpDebugHandler();
    }
    _theApplicationFactory.EnsureInited();
    _theApplicationFactory.EnsureAppStartCalled(context);
    return _theApplicationFactory.GetNormalApplicationInstance(context);
}

 

1. 先来看一下EnsureInited方法, 看看里面做了些什么.

private void EnsureInited()
{
    if (!this._inited)
    {
        lock (this)
        {
            if (!this._inited)
            {
                this.Init();
                this._inited = true;
            }
        }
    }
}

加锁的方式, 初始化了一些东西, 但是从这里并不能看出是初始化了什么, 那只能再进去看Init()方法了.

private void Init()
{
    if (_customApplication == null)
    {
        try
        {
            try
            {
                this._appFilename = GetApplicationFile();
                this.CompileApplication();
            }
            finally
            {
                this.SetupChangesMonitor();
            }
        }
        catch
        {
            throw;
        }
    }
} 

大叔在这一步就告诉我们, 是从global.asax获取内容, 进行编译. 但是并不能直观看到文件, 所以, 我决定再进一步瞧瞧.

1.1 GetApplicationFile 方法

internal static string GetApplicationFile()
{
    return Path.Combine(HttpRuntime.AppDomainAppPathInternal, "global.asax");
}

这一下, 就非常清晰的能看到, 确实出现了global.asax了. 到这里, 我才能确定, 大叔说的正确性. 也就是说, 这个方法, 其实是返回 global.asax文件的路径的.

OK, 既然已经拿到文件了, 那么在看一下, 接下来做了什么.

 

1.2 CompileApplication 方法 -- 编译Global.asax文件, 提取其中重要的信息

private void CompileApplication()
{
   //这里返回的是Global.asax中继承 HttpApplication类的类型, MVC模式下, 这里返回的是 typeof(MvcApplication)
   //如果查找不到, 则会返回 typeof(HttpApplication)
this._theApplicationType = BuildManager.GetGlobalAsaxType(); BuildResultCompiledGlobalAsaxType globalAsaxBuildResult = BuildManager.GetGlobalAsaxBuildResult(); if (globalAsaxBuildResult != null) { if (globalAsaxBuildResult.HasAppOrSessionObjects) { this.GetAppStateByParsingGlobalAsax(); } this._fileDependencies = globalAsaxBuildResult.VirtualPathDependencies; } if (this._state == null) { this._state = new HttpApplicationState(); } this.ReflectOnApplicationType(); }

1.2.1 继续看里面的 ReflectOnApplicationType 方法

private void ReflectOnApplicationType()
{
    ArrayList list = new ArrayList();
    foreach (MethodInfo info in this._theApplicationType.GetMethods(
    BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance)) { if (this.ReflectOnMethodInfoIfItLooksLikeEventHandler(info)) { list.Add(info); } } Type baseType = this._theApplicationType.BaseType; if ((baseType != null) && (baseType != typeof(HttpApplication))) { foreach (MethodInfo info2 in baseType.GetMethods(
      BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance)) { if (info2.IsPrivate && this.ReflectOnMethodInfoIfItLooksLikeEventHandler(info2)) { list.Add(info2); } } } this._eventHandlerMethods = new MethodInfo[list.Count]; for (int i = 0; i < this._eventHandlerMethods.Length; i++) { this._eventHandlerMethods[i] = (MethodInfo) list[i]; } }

接下来, 可以看到激动人心的代码了.  是什么呢? 答案即将揭晓.

1.2.1.1 ReflectOnMethodInfoIfItLooksLikeEventHandler方法

//HttpApplicationFactory类中的私有方法
private
bool ReflectOnMethodInfoIfItLooksLikeEventHandler(MethodInfo m) { ParameterInfo[] parameters; string str; if (m.ReturnType == typeof(void)) { parameters = m.GetParameters(); switch (parameters.Length) { case 0: goto Label_0089; case 2: if (!(parameters[0].ParameterType != typeof(object))) { if ((parameters[1].ParameterType != typeof(EventArgs)) && !parameters[1].ParameterType.IsSubclassOf(typeof(EventArgs))) { return false; } goto Label_0089; } return false; } } return false; Label_0089: str = m.Name; int index = str.IndexOf('_'); if ((index <= 0) || (index > (str.Length - 1))) { return false; } if (StringUtil.EqualsIgnoreCase(str, "Application_OnStart")
    || StringUtil.EqualsIgnoreCase(str, "Application_Start")) { this._onStartMethod = m; this._onStartParamCount = parameters.Length; } else if (StringUtil.EqualsIgnoreCase(str, "Application_OnEnd")
    || StringUtil.EqualsIgnoreCase(str, "Application_End")) { this._onEndMethod = m; this._onEndParamCount = parameters.Length; } else if (StringUtil.EqualsIgnoreCase(str, "Session_OnEnd")
    || StringUtil.EqualsIgnoreCase(str, "Session_End")) { this._sessionOnEndMethod = m; this._sessionOnEndParamCount = parameters.Length; } return true; }

看到我标红的六个名字了么. 是不是很熟悉, 是不是让人激动.

学过java的人, 可能对 EqualsIgnoreCase 比较熟悉, 这个方法, 是用于字符串的比较的, 忽略大小写, 比较字符串. 明白这个方法之后, 上面的代码应该就很清晰了.

好了, 看完这里, 就能明白, 在编译Global.asax的时候, 会获取其中的以上几个方法, 将之存入到 HttpApplicationFactory._eventHandlerMethods中.

 

1.3 SetupChangesMonitor方法

这个方法, 说实话, 我也没怎么看懂, 里面出现比较多的字样是 file changes, 但是就 Monitor字面意思来看, 应该是设置监视器的意思. 具体是干啥的, 暂时还没有弄清楚. 不过并不影响整个解析过程, 等之后我搞清楚了, 在贴在这里.

 

那在这里可以得出一个结论了 : HttpApplicationFactory._theApplicationFactory.EnsureInited()  的方法首先检查HttpApplicationFactory是否被初始化,如果没有,就通过HttpApplicationFactory.Init()进行初始化。在Init()中,先获取global.asax文件的完整路径,然后调用CompileApplication()对global.asax进行编译。注意哦, 这里是编译, 并不是执行里面的方法.

 

2. EnsureAppStartCalled 方法, 还是先贴出完整的代码

private void EnsureAppStartCalled(HttpContext context)
{
    if (!this._appOnStartCalled)
    {
        lock (this)
        {
            if (!this._appOnStartCalled)
            {
                using (new DisposableHttpContextWrapper(context))
                {
                    WebBaseEvent.RaiseSystemEvent(this, 0x3e9);
                    this.FireApplicationOnStart(context);
                }
                this._appOnStartCalled = true;
            }
        }
    }
}

一堆看不明白的代码, 没关系, 抓住重点的几个方法就可以了. \接下来, 看一下 FireApplicationOnStart 方法.

2.1 FireApplicationOnStart 方法

private void FireApplicationOnStart(HttpContext context)
{
    if (this._onStartMethod != null)
    {
        HttpApplication specialApplicationInstance = this.GetSpecialApplicationInstance();
        specialApplicationInstance.ProcessSpecialRequest(context, 
        
this._onStartMethod, this._onStartParamCount, this, EventArgs.Empty, null); this.RecycleSpecialApplicationInstance(specialApplicationInstance); } }

通过代码, 能看到, 这里又创建了一个 特殊的 HttpApplication 实例, 并且用一下之后, 就把他给回收了. 那么在回收之前, 又干了些什么事情呢?

注意到这里的 ProcessSpecialRequest 的参数中, 有一个方法 onStartMethod, 那么只要搞清楚, 这个方法指向谁, 就能知道 ProcessSpecialRequest  方法究竟干了些什么事情.

细心的人可能会发现, onStartMethod 看着有些熟悉, 其实他就是我在 1.2.1.1 中, 代码标注为绿色中的一个. 

 

2.1.1 ProcessSpecialRequest 方法

这里主要看一下这个方法吧. 因为在这个方法里面, 会调用 onStartMethod方法, 即 Application_Start 方法.

//HttpApplication
internal
void ProcessSpecialRequest(HttpContext context, MethodInfo method,
  int paramCount, object eventSource, EventArgs eventArgs, HttpSessionState session) { this._context = context; if (HttpRuntime.UseIntegratedPipeline && (this._context != null)) { this._context.HideRequestResponse = true; } this._hideRequestResponse = true; this._session = session; this._lastError = null; using (new DisposableHttpContextWrapper(context)) { using (new ApplicationImpersonationContext()) { try { this.SetAppLevelCulture(); this.InvokeMethodWithAssert(method, paramCount, eventSource, eventArgs); } catch (Exception exception) { Exception innerException; if (exception is TargetInvocationException) { innerException = exception.InnerException; } else { innerException = exception; } this.RecordError(innerException); if (context == null) { try { WebBaseEvent.RaiseRuntimeError(innerException, this); } catch { } } } finally { if (this._state != null) { this._state.EnsureUnLock(); } this.RestoreAppLevelCulture(); if (HttpRuntime.UseIntegratedPipeline && (this._context != null)) { this._context.HideRequestResponse = false; } this._hideRequestResponse = false; this._context = null; this._session = null; this._lastError = null; this._appEvent = null; } } } }

接着看上面这个标红方法.

[ReflectionPermission(SecurityAction.Assert, Flags=ReflectionPermissionFlag.RestrictedMemberAccess)]
private void InvokeMethodWithAssert(MethodInfo method, int paramCount, object eventSource, EventArgs eventArgs)
{
    if (paramCount == 0)
    {
        method.Invoke(this, new object[0]);
    }
    else
    {
        method.Invoke(this, new object[] { eventSource, eventArgs });
    }
}

在这里看到, 调用方法了. 现在知道 Application_Start的执行时机了.

 

OK, 到这里, 可以得到我们想要的结论: HttpApplicationFactory._theApplicationFactory.EnsureAppStartCalled(context)  创建特定的HttpApplication实例,触发ApplicationOnStart事件,执行ASP.global_asax中的Application_Start(object sender, EventArgs e)方法。然后在处理完事件以后就立即被回收掉,因为系统初始化只需要一次. 

 

3. GetNormalApplicationInstance 方法, 代码如下:

private HttpApplication GetNormalApplicationInstance(HttpContext context)
{
    HttpApplication state = null;
    lock (this._freeList)
    {
        if (this._numFreeAppInstances > 0)
        {
            state = (HttpApplication) this._freeList.Pop();
            this._numFreeAppInstances--;
            if (this._numFreeAppInstances < this._minFreeAppInstances)
            {
                this._minFreeAppInstances = this._numFreeAppInstances;
            }
        }
    }
    if (state == null)
    {
        state = (HttpApplication) HttpRuntime.CreateNonPublicInstance(this._theApplicationType);
        using (new ApplicationImpersonationContext())
        {
            state.InitInternal(context, this._state, this._eventHandlerMethods);
        }
    }
    if (AppSettings.UseTaskFriendlySynchronizationContext)
    {
        state.ApplicationInstanceConsumersCounter = new CountdownTask(1);
        state.ApplicationInstanceConsumersCounter.Task.ContinueWith(delegate (Task _, object o) {
            RecycleApplicationInstance((HttpApplication) o);
        }, state, TaskContinuationOptions.ExecuteSynchronously);
    }
    return state;
}

 从这个方法可以看出, 并不是每次都会去创建 HttpApplication 实例的, 而是会先去查看是否有空闲的实例, 有的话, 就直接用, 没有才会去创建一个新的.

在拿到这个实例之后, 调用了 InitInternal 方法. 那么来看一下这个方法里面, 又做了些什么.

internal void InitInternal(HttpContext context, HttpApplicationState state, MethodInfo[] handlers)
{
    this._state = state;
    PerfCounters.IncrementCounter(AppPerfCounter.PIPELINES);
    try
    {
        try
        {
            // Remember context for config lookups
            this._initContext = context;
            this._initContext.ApplicationInstance = this;
            // Set config path to be application path for the application initialization
            context.ConfigurationPath = context.Request.ApplicationPathObject;
            // keep HttpContext.Current working while running user code
            using (new DisposableHttpContextWrapper(context))
            {
                // Build module list from config
                if (HttpRuntime.UseIntegratedPipeline)
                {
             //集成模式下, 走这里, 会跳过 InitModules()方法
try { context.HideRequestResponse = true; this._hideRequestResponse = true; this.InitIntegratedModules(); goto Label_006B; } finally { context.HideRequestResponse = false; this._hideRequestResponse = false; } }
          //经典模式下, 才会进这个方法
this.InitModules(); Label_006B: if (handlers != null) { this.HookupEventHandlersForApplicationAndModules(handlers); } this._context = context; if (HttpRuntime.UseIntegratedPipeline && (this._context != null)) { this._context.HideRequestResponse = true; } this._hideRequestResponse = true; try {
             //虚方法, 调用MVCApplication的Init方法(如果这个方法存在), 否则调用HttpApplication的Init方法
this.Init(); } catch (Exception exception) { this.RecordError(exception); } } if (HttpRuntime.UseIntegratedPipeline && (this._context != null)) { this._context.HideRequestResponse = false; } this._hideRequestResponse = false; this._context = null; this._resumeStepsWaitCallback = new WaitCallback(this.ResumeStepsWaitCallback);
       //Construct the execution steps array
if (HttpRuntime.UseIntegratedPipeline) { this._stepManager = new PipelineStepManager(this); } else { this._stepManager = new ApplicationStepManager(this); } this._stepManager.BuildSteps(this._resumeStepsWaitCallback); } finally { this._initInternalCompleted = true;
        //Reset config path context.ConfigurationPath
= null;
        //don't hold on to the context
this._initContext.ApplicationInstance = null; this._initContext = null; } } catch { throw; } }

这个方法好长啊, 不过没关系, 后面会详细讲解. 先从字面意思看, 这里应该是初始化了 HttpModules , 然后通过 BuildSteps 来创建 20多个生命周期事件的处理函数(后面会详细介绍). 

 

到这里, 我们先总结一下再看代码,InitInternal方法的主要功能如下:

  1. InitModules():经典模式下, 加载Web.config配置中的HttpModules 以及 动态注册的HttpModules。
  2. InitIntegratedModules():集成模式下, 加载在服务器上设定的HttpModuels和Web.config里system.webserver下的HttpModuels。
  3. HookupEventHandlersForApplicationAndModules:根据发生的事件,调用HttpApplication实例中相应的事件处理函数。
  4. 创建很多实现IExecutionStep接口的类的实例并添加到当前HttpApplication实例的_execSteps中,等待回调时执行。从这里我们可以看到HttpApplication是以异步的方式处理请求, 对请求的很多处理工作都放入了_execStep等待回调时执行。

至此,除了20多个周期事件和Handler相关的代码我们没有讲解,其它和HttpApplication相关的并且对我们有帮助的,已经差不多清晰了。关于20多个周期事件和执行Handler方面的内容,我们下一章节再做详细解释。

 

转载参考:

   MVC之前的那点事儿

 

目录已同步

转载于:https://www.cnblogs.com/elvinle/p/6268262.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值