且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Asp.net 4.0,首次请求目录下的文件时响应很慢

更新时间:2022-09-13 12:03:45

1. 问题起因
2. 尝试过的处理思路
3. 解决方法

 

1. 问题起因

    一个从VS2003(.Net Framework 1.1)升级到.net framework 4.0的项目,每次编译或者部署到服务器上后,首次请求任何一个目录下的默认页面时,都要耗时3~5秒;而以前使用.net framework 1.1的时候,没有这个问题。

我在页面上开启Trace="true"来跟踪,发现页面的处理时间并不久(IIS重启,首次打开页面时截获的信息):Asp.net 4.0,首次请求目录下的文件时响应很慢

  但是IIS日志中,显示首次请求页面的耗时接近4.635秒。后续连续刷新页面,处理时间比较正常。。

Asp.net 4.0,首次请求目录下的文件时响应很慢

虽然耗时比较久,但页面能够打开;然后我重启IIS,再次打开站点,用VS附加W3WP.exe来进行调试,再次打开这个页面的时候,发现抛出异常了:发生了 System.ArgumentException, Message=已存在具有相同键的条目。

   1: 发生了 System.ArgumentException
   2:   Message=已存在具有相同键的条目。
   3:   Source=System
   4:   StackTrace:
   5:        在 System.Collections.Specialized.ListDictionary.Add(Object key, Object value)
   6:   InnerException: 
   7:     System.dll!System.Collections.Specialized.ListDictionary.Add(object key, object value) + 0x134 字节    
   8:      System.Web.dll!System.Web.UI.ParsedAttributeCollection.AddFilteredAttribute(string filter, string name, string value) + 0xba 字节    
   9:      System.Web.dll!System.Web.UI.TemplateParser.ProcessAttributes(System.Text.RegularExpressions.Match match, out System.Web.UI.ParsedAttributeCollection attribs = {System.Web.UI.ParsedAttributeCollection}, bool fDirective = false, out string duplicateAttribute = null) + 0x221 字节    
  10:      System.Web.dll!System.Web.UI.TemplateParser.ProcessBeginTag(System.Text.RegularExpressions.Match match = {System.Text.RegularExpressions.Match}, string inputText) + 0x68 字节    
  11:      System.Web.dll!System.Web.UI.TemplateParser.ParseStringInternal(string text, System.Text.Encoding fileEncoding) + 0x3d0 字节    
  12:      System.Web.dll!System.Web.UI.TemplateParser.ParseString(string text, System.Web.VirtualPath virtualPath, System.Text.Encoding fileEncoding) + 0x6f 字节    
  13:      System.Web.dll!System.Web.UI.TemplateParser.ParseFile(string physicalPath, System.Web.VirtualPath virtualPath) + 0x115 字节    
  14:      System.Web.dll!System.Web.UI.TemplateParser.ParseInternal() + 0x57 字节    
  15:      System.Web.dll!System.Web.UI.TemplateParser.Parse() + 0x64 字节    
  16:      System.Web.dll!System.Web.Compilation.BaseTemplateBuildProvider.CodeCompilerType.get() + 0x6f 字节    
  17:      System.Web.dll!System.Web.Compilation.BuildProvider.GetCompilerTypeFromBuildProvider(System.Web.Compilation.BuildProvider buildProvider) + 0x42 字节    
  18:      System.Web.dll!System.Web.Compilation.WebDirectoryBatchCompiler.CompileNonDependentBuildProviders(System.Collections.ICollection buildProviders) + 0xca 字节    
  19:      System.Web.dll!System.Web.Compilation.WebDirectoryBatchCompiler.Process() + 0x5d 字节    
  20:      System.Web.dll!System.Web.Compilation.BuildManager.BatchCompileWebDirectoryInternal(System.Web.Hosting.VirtualDirectory vdir, bool ignoreErrors) + 0x48 字节    
  21:      System.Web.dll!System.Web.Compilation.BuildManager.BatchCompileWebDirectory(System.Web.Hosting.VirtualDirectory vdir, System.Web.VirtualPath virtualDir, bool ignoreErrors) + 0xbc 字节    
  22:      System.Web.dll!System.Web.Compilation.BuildManager.CompileWebFile(System.Web.VirtualPath virtualPath = {System.Web.VirtualPath}) + 0x5d 字节    
  23:      System.Web.dll!System.Web.Compilation.BuildManager.GetVPathBuildResultInternal(System.Web.VirtualPath virtualPath = {System.Web.VirtualPath}, bool noBuild, bool allowCrossApp, bool allowBuildInPrecompile, bool throwIfNotFound, bool ensureIsUpToDate) + 0x141 字节    
  24:      System.Web.dll!System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert(System.Web.HttpContext context, System.Web.VirtualPath virtualPath, bool noBuild, bool allowCrossApp, bool allowBuildInPrecompile, bool throwIfNotFound, bool ensureIsUpToDate) + 0x70 字节    
  25:      System.Web.dll!System.Web.Compilation.BuildManager.GetVirtualPathObjectFactory(System.Web.VirtualPath virtualPath, System.Web.HttpContext context, bool allowCrossApp, bool throwIfNotFound) + 0x7e 字节    
  26:      System.Web.dll!System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(System.Web.VirtualPath virtualPath, System.Type requiredBaseType = {Name = "Page" FullName = "System.Web.UI.Page"}, System.Web.HttpContext context = {System.Web.HttpContext}, bool allowCrossApp) + 0x35 字节    
  27:      System.Web.dll!System.Web.UI.PageHandlerFactory.GetHandlerHelper(System.Web.HttpContext context, string requestType, System.Web.VirtualPath virtualPath = {System.Web.VirtualPath}, string physicalPath) + 0x20 字节    
  28:      System.Web.dll!System.Web.UI.PageHandlerFactory.System.Web.IHttpHandlerFactory2.GetHandler(System.Web.HttpContext context, string requestType, System.Web.VirtualPath virtualPath, string physicalPath) + 0x29 字节    
  29:      System.Web.dll!System.Web.HttpApplication.MapHttpHandler(System.Web.HttpContext context, string requestType, System.Web.VirtualPath path, string pathTranslated = "。。。。。。。。。。\\order\\default.aspx", bool useAppConfig) + 0xa8 字节    
  30:      System.Web.dll!System.Web.HttpApplication.MapHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() + 0x81 字节    
  31:      System.Web.dll!System.Web.HttpApplication.ExecuteStep(System.Web.HttpApplication.IExecutionStep step = {System.Web.HttpApplication.MapHandlerExecutionStep}, ref bool completedSynchronously = true) + 0xb9 字节    
  32:      System.Web.dll!System.Web.HttpApplication.ApplicationStepManager.ResumeSteps(System.Exception error) + 0x13e 字节    
  33:      System.Web.dll!System.Web.HttpApplication.System.Web.IHttpAsyncHandler.BeginProcessRequest(System.Web.HttpContext context, System.AsyncCallback cb, object extraData) + 0xf8 字节    
  34:      System.Web.dll!System.Web.HttpRuntime.ProcessRequestInternal(System.Web.HttpWorkerRequest wr = {System.Web.Hosting.ISAPIWorkerRequestInProcForIIS7}) + 0x1a2 字节    
  35:      System.Web.dll!System.Web.HttpRuntime.ProcessRequestNoDemand(System.Web.HttpWorkerRequest wr) + 0x7d 字节    
  36:      System.Web.dll!System.Web.Hosting.ISAPIRuntime.ProcessRequest(System.IntPtr ecb, int iWRType) + 0xfd 字节    
  37:      [应用程序域转换]    
  38:      [本机到托管的转换]   

 

2.  尝试过的处理思路

    我Attach到w3wp上去后,首次访问目录下的默认页面,catch到ArgumentException异常,然后根据异常信息,追踪到 TemplateParser.ParseFile -> ParseString -> ParseStringInternal -> ProcessBeginTag;然后我就尝试了下列努力:

1. 该目录下默认文件的<%@Page ....%>定义,属性与其他文件并没有任何不同(除了CodeBehind和Inherits的值),我把页面里面的内容一点儿一点儿删掉,最后是剩下空内容(只剩下head、body、form几个元素)、空后台代码,问题还依旧存在。

2. 我在这个目录下面,新建了一个页面(VS2010自动生成的,没有添加任何元素或标签),然后把这个新文件作为该目录默认页,发现问题还是存在。

3. 在第2步的基础上,打开新默认页后,我在URL中直接输入旧的默认页,页面瞬间刷出来了。然后我就纠结了,这延时,与页面内容毛的关系都木有,这异常信息也太坑爹了。

4. 出于尝试一下,我试下该目录下的其他文件,首次访问会不会变慢。于是重启IIS,然后发现首次访问该目录中的第一个文件(不论首次访问的是哪个文件),都会耗时数秒;如果Attach到w3wp,都会Catch到这个异常。

5. 项目是从.net framework 1.1(VS2003)上直接升级过来的,以前03的版本,木有这个问题。然后google“asp.net 4.0 slow”,找到下面这个:

    Slow Performance — ASP .NET ASPNET_WP.EXE and CSC.EXE Running After Clicking Redirect Link

6. The very first time that the page is load, then the asp.net compile a lot of pages, almost every one found on the same dir, including modules, and dlls found on bin.也就是说,在首次请求一个目录下的某个文件时,会把该目录的所有文件都编译一下。这样做的好处是,把一个目录的编译操作都集中在首次访问上,后续请求会比较快;但这样带来的问题是,如果编译时间比较久,那第一个发起请求的人就杯具了。于是在配置文件中,设置batch="false",然后问题消失,首次请求的速度提上来了,attach上去也没有异常了。

   

3.  解决方法

   部署的时候,记得在配置文件中,把debug和batch关闭,把optimizeCompilations打开:

    <compilation targetFramework="4.0" debug="false" batch="false" optimizeCompilations="true">

 


happyhippy作者:Silent Void 
出处:http://happyhippy.cnblogs.com/
转载须保留此声明,并注明在文章起始位置给出原文链接。
 
本文转自Silent Void博客园博客,原文链接:http://www.cnblogs.com/happyhippy/archive/2012/03/15/2397901.html,如需转载请自行联系原作者