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

简介:

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

 

1. 问题起因

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

我在页面上开启Trace="true"来跟踪,发现页面的处理时间并不久(IIS重启,首次打开页面时截获的信息):

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

虽然耗时比较久,但页面能够打开;然后我重启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 ,如需转载请自行联系原作者
相关文章
|
11天前
|
C# Windows
.NET开源免费的Windows快速文件搜索和应用程序启动器
今天大姚给大家分享一款.NET开源(MIT License)、免费、功能强大的Windows快速文件搜索和应用程序启动器:Flow Launcher。
|
4月前
|
JSON 开发框架 .NET
ASP.NET Core Web API设置响应输出的Json数据格式的两种方式
ASP.NET Core Web API设置响应输出的Json数据格式的两种方式
|
6月前
|
对象存储
.net core 阿里云接口之拷贝文件
紧接上文, 1)[.net core 阿里云接口之获取临时访问凭证](https://developer.aliyun.com/article/1363447?spm=a2c6h.12873639.article-detail.7.2b0e5b1cpeWbZ5 ".net core 阿里云接口之获取临时访问凭证") 2)[.net core 阿里云接口之将指定的OSS文件下载到流](https://developer.aliyun.com/article/1363886 ".net core 阿里云接口之将指定的OSS文件下载到流") 本文继续阿里云接口调用,将指定的OSS文件下载到流。
32 0
|
6月前
|
C#
.net core 从(本地)服务器获取APK文件并解析APK信息
## 1、apk解析除了使用客户端利用aapt.exe、unzip.exe开发客户端解析外,还可以直接利用服务进行解析 ```csharp /// <summary> /// 从本地服务器获取APK文件并解析APK信息 /// </summary> /// <param name="fileName">APK文件的完整路径</param> /// <returns></returns> [HttpPost, HttpGet, HttpOptions, CorsOptions] public IActionResult DecodeAPK(string fileName) { if(fi
29 0
|
6月前
|
JavaScript 前端开发 关系型数据库
.net core + vue + elementui 删除指定日期段、指定路径下的所有文件
# 1、呈现效果 ![image.png](https://ucc.alicdn.com/pic/developer-ecology/j2ygdazy447va_0782583bbc894c33a079db9e44385acd.png) # 2、后端 ## 1)服务层 ```csharp /// <summary> /// 删除指定修改日期段及指定路径下的所有文件 /// </summary> /// <param name="filepath">指定路径</param> /// <returns>返回删除结果提示</returns> public string DeleteSpecif
40 0
|
6月前
|
C#
.net core 删除指定路径下的所有文件以及文件夹(文件夹建议保留目录)
1、服务层 ```csharp /// <summary> /// 删除指定路径下的所有文件 /// </summary> /// <param name="filepath">指定路径</param> /// <returns></returns> public string DeleteSpecifiedPathAllFile(string filepath) { try { DirectoryInfo info = new DirectoryInfo(filepath); // 去除文件夹的只读属性 info.Attribu
27 0
|
6月前
|
开发框架 JSON .NET
ASP.NET Core 响应location
响应中 location 的设置示例:发送POST 请求,进行创建资源的操作。注意的请求头中content-type 的设置,需要设置成 application/json 类型,并不一定需要json 类型的数据,但默认情况下使用的都是json传输数据,否则asp.net core 会返回 415 状态码。同时请求体中 也要符合api 接口需要的格式,如果不符合,则会得到 400 的响应码。...
36 0
ASP.NET Core 响应location
|
7月前
|
开发框架 JSON Kubernetes
.NET Core - 环境变量配置和文件提供程序配置方式详解
.NET Core - 环境变量配置和文件提供程序配置方式详解
|
8月前
|
开发框架 前端开发 .NET
asp.net 文件分片上传
asp.net 文件分片上传
99 0
asp.net 文件分片上传
|
9月前
|
数据库 C#
C#,.net,winform导入Excel功能以及下载Excel文件到本地,并使用SqlBulkCopy把DataTable类型的数据写入到sqlserver数据库中
C#,.net,winform导入Excel功能以及下载Excel文件到本地,并使用SqlBulkCopy把DataTable类型的数据写入到sqlserver数据库中
215 0