我的控制器之前ASP.NET MVC在做什么?

6
我在我的MVC控制器中遇到了一个奇怪的性能问题,或者说是在它之前?
根据Mini Profiler的输出,在到达我的控制器之前有120毫秒的开销。
有人知道这是为什么吗?这是在一个服务器上(不是本地),已经设置了Compilation debug=false,所以不是没有在release模式下运行的问题。
在它之后的所有东西,我都可以调整/修改,但在它之前?我迷失了...
有什么想法吗?

enter image description here

更新

在进行一些性能工具测试后,我发现了输入链接描述输入链接描述,结果如下:

最昂贵的堆栈 ------------------------------------ System.Web.HttpApplication+CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute System.Web.HttpApplication.ExecuteStep System.Web.HttpApplication+PipelineStepManager.ResumeSteps System.Web.HttpApplication.BeginProcessRequestNotification System.Web.HttpRuntime.ProcessRequestNotificationPrivate System.Web.Hosting.PipelineRuntime.ProcessRequestNotificationHelper System.Web.Hosting.PipelineRuntime.ProcessRequestNotification System.Web.Hosting.UnsafeIISMethods.MgdIndicateCompletion System.Web.Hosting.PipelineRuntime.ProcessRequestNotificationHelper System.Web.Hosting.PipelineRuntime.ProcessRequestNotification ===> 花费 (1716011)
Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp Microsoft.Practices.ObjectBuilder2.BuilderContext.NewBuildUp Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp Microsoft.Practices.ObjectBuilder2.BuilderContext.NewBuildUp Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp Microsoft.Practices.ObjectBuilder2.BuilderContext.NewBuildUp Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp Microsoft.Practices.Unity.UnityContainer.DoBuildUp Microsoft.Practices.Unity.UnityContainer.DoBuildUp System.Web.Mvc.DefaultControllerFactory+DefaultControllerActivator.Create System.Web.Mvc.DefaultControllerFactory.CreateController System.Web.Mvc.MvcHandler.ProcessRequestInit System.Web.Mvc.MvcHandler+<>c__DisplayClass6.b__2 System.Web.Mvc.SecurityUtil+<>c__DisplayClassb`1.b__a System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust System.Web.HttpApplication+CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute System.Web.HttpApplication.ExecuteStep System.Web.HttpApplication+PipelineStepManager.ResumeSteps System.Web.HttpApplication.BeginProcessRequestNotification System.Web.HttpRuntime.ProcessRequestNotificationPrivate System.Web.Hosting.PipelineRuntime.ProcessRequestNotificationHelper System.Web.Hosting.PipelineRuntime.ProcessRequestNotification System.Web.Hosting.UnsafeIISMethods.MgdIndicateCompletion System.Web.Hosting.PipelineRuntime.ProcessRequestNotificationHelper System.Web.Hosting.PipelineRuntime.ProcessRequestNotification ===> 花费 (936006)
Microsoft.Win32.Win32Native.CoCreateGuid StackExchange.Profiling.Timing..ctor StackExchange.Profiling.MVCHelpers.ProfilingViewEngine.Find System.Web.Mvc.ViewEngineCollection+<>c__DisplayClassc.b__a System.Web.Mvc.ViewEngineCollection.Find System.Web.Mvc.ViewEngineCollection.Find System.Web.Mvc.ViewResult.FindView System.Web.Mvc.ViewResultBase.ExecuteResult System.Web.Mvc.ControllerActionInvoker+<>c__DisplayClass1c.b__19 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters System.Web.Mvc.ControllerActionInvoker.InvokeAction System.Web.Mvc.Controller.ExecuteCore System.Web.Mvc.ControllerBase.Execute System.Web.Mvc.MvcHandler+<>c__DisplayClass6+<>c__DisplayClassb.b__5 System.Web.Mvc.Async.AsyncResultWrapper+<>c__DisplayClass1.b__0 System.Web.Mvc.MvcHandler+<>c__DisplayClasse.b__d System.Web.HttpApplication+CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute System.Web.HttpApplication.ExecuteStep System.Web.HttpApplication+PipelineStepManager.ResumeSteps System.Web.HttpApplication.BeginProcessRequestNotification System.Web.HttpRuntime.ProcessRequestNotificationPrivate System.Web.Hosting.PipelineRuntime.ProcessRequestNotificationHelper System.Web.Hosting.PipelineRuntime.ProcessRequestNotification System.Web.Hosting.UnsafeIISMethods.MgdIndicateCompletion System.Web.Hosting.PipelineRuntime.ProcessRequestNotificationHelper System.Web.Hosting.PipelineRuntime.ProcessRequestNotification ===> 花费 (780005)

Unity是否可能引起一些问题?


这种延迟问题是否也发生在其他控制器上,还是只有这个控制器有延迟? - Alex
它只在这个特定的控制器上发生,我的另一个调用相同 BL 的控制器(和其他周围逻辑)速度要快得多! - Stuart.Sklinar
我明白了...我原本期望有一致的行为并打算指向服务器本身,但现在我不知所措了。 - Alex
它唯一的一致性就是它总是那个控制器方法。 - Stuart.Sklinar
2个回答

1

您应该查看The ASP.NET MVC Pipeline

ASP.NET MVC管道可以分为以下几个部分:

  • 应用初始化 - 在此方法中,您可以将Route对象添加到静态的RouteTable.Routes集合(类型为RouteCollection)中。

  • 路由部分 - 路由模块尝试将传入的URL与路由表匹配,并调用相应的IRouteHandler。

  • 控制器创建 - IControllerFactory根据路由参数和默认命名约定创建控制器的实例。

  • 动作执行 - IActionInvoker识别要执行的方法,IModelBinder验证和绑定方法参数,IFilterProvider发现要应用的过滤器。操作返回ActionResult类型。

  • 视图 - IViewEngine实例化正确的视图引擎,并将模型传递给视图。使用模型验证提供程序,检索验证规则以创建客户端验证脚本和远程验证脚本。

了解更多信息:

http://blog.stevensanderson.com/2007/11/20/aspnet-mvc-pipeline-lifecycle/

你认为这可能是Unity引起的问题吗? - Stuart.Sklinar

0

对我的英语表示抱歉。

有许多方面需要注意:
1.在全局或某些HttpModule中使用Application_BeginRequest(和AuthenticateRequest)
2.过滤器
3.MVC框架、控制器工厂、操作调用程序等等
4.第一次运行时,将IL编译为本机代码需要花费很多时间

我认为您可以使用秒表来检查您的操作中代码所花费的时间,以便找出哪个点花费了太多时间。

120毫秒不是运行操作之前花费的时间,我认为这是操作完成的时间。

顺便说一句,120毫秒并不算太糟糕。

更新

public ActionResult Index()
{
    var profiler = MiniProfiler.Current;
    using (profiler.Step("action"))
    {
        return View();
    }
}

在此输入图片描述
这张图片是上面代码的运行结果
你可以看到:
action只花费了1.9,是一个子级
而http://......花费了302,它包括控制器和action

所以你应该检查你的action代码内部或外部的时间成本
在图片中,它花费了300+,因为这是第一次运行,第二次运行只花费了4
第一次运行必须很慢


正如分析器所显示的那样,这是在我的代码之前发生的,因此我无法使用秒表计时。 - Stuart.Sklinar
不,这不是第一次运行,也不是我的操作代码有问题,从图片上可以看到,我的代码在20ms内正常运行,问题出在HTTP上。 - Stuart.Sklinar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接