OneAPM 发表于 2016-1-13 13:04:08

11个显著提升 ASP.NET 应用程序性能的技巧——第1部分

本帖最后由 OneAPM 于 2016-1-13 13:05 编辑

【编者按】本文出自站外作者 Brij Bhushan Mishra ,Brij 是微软 MVP-ASP.NET/IIS、C# Corner MVP、CodeProject Insider,前 CodeProject MVP,CodeProject Mentor 以及 CodeProject Platinum Member,拥有6年左右的高级开发工程师/架构师经验,自幼酷爱计算机。采用 ASP.NET 和 IIS 构建 Web 应用程序并将其托管到 Web 服务器上极其简单,但是许多可提升 Web 应用程序性能的机会或隐藏配置同样不能忽视。本系列博文将介绍一些简单但却可以应用于任何 Web 应用程序的技巧,而它们却是经常被忽略或遗忘的。
1- 内核模式缓存——这是广泛用于程序编写的主要工具之一,可加速 Web 应用程序。但是大多数时候,很少开发者以最佳方式应用内核模式缓存,仅仅发挥其部分主要优势。由于所有 ASP.NET 请求均会经历不同阶段,因此可在不同级别使用缓存,具体如下所示。http://www.infragistics.com/community/cfs-filesystemfile.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/devtoolsguy.Maria_5F00_Blogs/8206.Brij_5F00_post1.png使用缓存的具体步骤如下:a)转到 IIS 并选择网站。
b)点击 IIS 部分正下方的Output Cache 图标。
c)点击右侧面板中 Actions 下方的 Add,出现以下对话框:http://www.infragistics.com/community/cfs-filesystemfile.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/devtoolsguy.Maria_5F00_Blogs/5657.Brij_5F00_post2.png
2- Pipeline 模式(IIS 7+可用)—经典模式和集成模式选择相应应用程序池并右击属性,便可设置/更新 Pipeline 模式。http://www.infragistics.com/community/cfs-filesystemfile.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/devtoolsguy.Maria_5F00_Blogs/4174.Brij_5F00_post3.png
3- 删除不用的模块—如下图所示对请求进行处理:http://www.infragistics.com/community/cfs-filesystemfile.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/devtoolsguy.Maria_5F00_Blogs/5141.Brij_5F00_post4.png通过添加以下代码得到所有模块列表:HttpApplication httpApps = HttpContext.ApplicationInstance;
//Get list of active modules
HttpModuleCollection httpModuleCollections = httpApps.Modules;
ViewBag.ModulesCount = httpModuleCollections.Count;
得到的模块可绑定至任何控件,具体结果如下:http://www.infragistics.com/community/cfs-filesystemfile.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/devtoolsguy.Maria_5F00_Blogs/8204.Brij_5F00_post5.png上图显示已启用18个模块,而其中有些模块并未真正使用,但是请求仍需通过所有模块。因此,可从 Pipeline 中删除不用的模块。只需在 web.config 中添加下列配置便可删除模块:<system.webServer>
    <modules>
      <remove name="FormsAuthentication" />
      <remove name="DefaultAuthentication" />
      <remove name="OutputCache" />
      <remove name="AnonymousIdentification" />
      <remove name="RoleManager" />
    modules>
system.webServer>

此处,采用删除标记列出需要删除的模块。由于在此删除了5个模块,下次查看现用模块便只有13个。
注:示例为2013版,如果使用其他版本,得到的模块数可能不相同,但重点是需删除不需要的模块。4- 为所有请求运行所有托管模块——这是存在于 web.config 或 applicationHost.config中的另一配置,通过以下代码设置对IIS 上所有应用程序有效。<modules runAllManagedModulesForAllRequests="true">

5- 删除多余的视图引擎——a)众所周知,视图引擎是 MVC 请求生命周期的一部分,且负责发现并处理视图。也可添加自定义的视图引擎。接下来创建默认的 MVC 应用程序并试图返回解决方案中不存在的视图。现在运行此应用程序会出现以下错误。http://www.infragistics.com/community/cfs-filesystemfile.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/devtoolsguy.Maria_5F00_Blogs/4075.Brij_5F00_post6.png采用 Application_Start 方法添加以下代码,Application_Start 方法在 Global.asax 内可用。            // Removing all the view engines
            ViewEngines.Engines.Clear();
            //Add Razor Engine (which we are using)
   ViewEngines.Engines.Add(new RazorViewEngine());
现在再次运行应用程序。http://www.infragistics.com/community/cfs-filesystemfile.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/devtoolsguy.Maria_5F00_Blogs/4075.Brij_5F00_post7.png现在应用程序只查找 razor 文件。b) 仔细看以上截图可发现应用程序在查找 c# 和 vb 文件,假设解决方案中从未使用 vb,因此查找 vbhtml 文件并无任何作用。欲解决这个问题,需要编写自定义的视图引擎。因此,按照以下方法编写自定义 razor 视图引擎: public class MyCustomViewEngine : RazorViewEngine
    {
      public MyCustomViewEngine()
      {
            base.AreaViewLocationFormats = new string[] {"~/Areas/{2}/Views/{1}/{0}.cshtml", "~/Areas/{2}/Views/Shared/{0}.cshtml"};
            base.AreaMasterLocationFormats = new string[] {"~/Areas/{2}/Views/{1}/{0}.cshtml", "~/Areas/{2}/Views/Shared/{0}.cshtml" };
            base.AreaPartialViewLocationFormats = new string[] {"~/Areas/{2}/Views/{1}/{0}.cshtml","~/Areas/{2}/Views/Shared/{0}.cshtml"};
            base.ViewLocationFormats = new string[] { "~/Views/{1}/{0}.cshtml","~/Views/Shared/{0}.cshtml" };
            base.MasterLocationFormats = new string[] { "~/Views/{1}/{0}.cshtml","~/Views/Shared/{0}.cshtml" };
            base.PartialViewLocationFormats = new string[] {"~/Views/{1}/{0}.cshtml", "~/Views/Shared/{0}.cshtml" };
            base.FileExtensions = new string[] { "cshtml" };
      }
    }

在此继承了 RazorViewEngine,如果看见代码中的构造函数,则会发现已指定所有可能存在文件的位置,也包括可能的文件扩展名。现在 Global.asax 内使用此视图引擎。
运行应用程序。http://www.infragistics.com/community/cfs-filesystemfile.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/devtoolsguy.Maria_5F00_Blogs/3010.Brij_5F00_post8.png
现在应用程序查找 csharp razor 文件,会获得不错的性能。
结论——本文介绍了可很容易用于任何 ASP.NET 应用程序的6个技巧:
1- 内核模式缓存2- Pipeline 模式3- 删除不用的模块4- 为所有请求运行所有托管模块5- 切勿在 wwwroot 内写入任何内容6- 删除未使用的视图引擎及语言
本系列后续博文将再介绍5个可用作应用程序性能提升器的技巧。敬请期待!
OneAPM 助您轻松锁定 .NET 应用性能瓶颈,通过强大的 Trace 记录逐层分析,直至锁定行级问题代码。以用户角度展示系统响应速度,以地域和浏览器维度统计用户使用情况。想阅读更多技术文章,请访问 OneAPM 官方博客。
原文地址:
http://www.infragistics.com/community/blogs/devtoolsguy/archive/2015/08/07/12-tips-to-increase-the-performance-of-asp-net-application-drastically-part-1.aspx
本文转自 OneAPM 官方博客
页: [1]
查看完整版本: 11个显著提升 ASP.NET 应用程序性能的技巧——第1部分