.NET performance optimization settings

Patrick Gidich edited this page Oct 7, 2015 · 15 revisions

Learn more about performance:

Table of Contents

Settings to Apply to Web.config

Main Web.conifg settings:

Tracing

Enable Tracing Learn More

 <system.web>
  <customErrors mode="Off"/>
  <trace enabled="true" pageOutput="false" localOnly="false" requestLimit="2000" writeToDiagnosticsTrace="true" />
 </system.web>
<system.diagnostics>
  <trace autoflush="true" indentsize="4">
    <listeners>
      <add name="WebPageTraceListener" traceOutputOptions="ThreadId" type="System.Web.WebPageTraceListener, System.Web, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    </listeners>
  </trace>
</system.diagnostics>

Be sure to turn off tracing / detailed logging in production: Production Web.config Transform:

<system.web>
  <compilation xdt:Transform="RemoveAttributes(debug)" />
  <trace xdt:Transform="SetAttributes(enabled)" enabled="false" />
  <customErrors xdt:Transform="SetAttributes(mode)" enabled="On" />
</system.web>

Removing Unneeded View Engines

http://blogs.msdn.com/b/marcinon/archive/2011/08/16/optimizing-mvc-view-lookup-performance.aspx

Precompiling ASP.NET MVC Views

http://blogs.msdn.com/b/jimlamb/archive/2010/04/20/turn-on-compile-time-view-checking-for-asp-net-mvc-projects-in-tfs-build-2010.aspx

Hiding IIS

 <system.web>
  <httpRuntime enableVersionHeader="false" />
 </system.web>
<system.webServer>
  <httpProtocol>
    <customHeaders>
      <remove name="X-Powered-By" />
    </customHeaders>
  </httpProtocol>
  <modules runAllManagedModulesForAllRequests="true">
    <add name="CloakHttpHeaderModule" type="PROJECTNAME.Web.CloakIIS.CloakHttpHeaderModule, PROJECTNAME.Web.CloakIIS, Version=1.0.0.0, Culture=neutral" />
  </modules>
</system.webServer>

(note the above CloakHttpHeaderModule should be implemented as defined here

Caching Output

 <system.web>
  <caching>
    <cache percentagePhysicalMemoryUsedLimit="60" />
    <outputCacheSettings>
      <outputCacheProfiles>
        <!-- 3600 = 1hr -->
        <add name="DefaultStaticCache" duration="3600" varyByParam="none"/>
      </outputCacheProfiles>
    </outputCacheSettings>
  </caching>
 </system.web>

Example of applying the output cache to a controller: Learn More

[OutputCache(CacheProfile = "DefaultStaticCache")]
public ActionResult Index() {
  return View();
}

If you have parameters to your method please use the following

[OutputCache(CacheProfile = "DefaultStaticCache", VaryByParam = "id")]
public ActionResult Index(string id) {
  return View();
}

Enabling Compression (IIS7+)

Read More

<system.webServer>
  <urlCompression doDynamicCompression="true" dynamicCompressionBeforeCache="true" />
  <httpCompression dynamicCompressionDisableCpuUsage="90" dynamicCompressionEnableCpuUsage="60">
    <scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" />
    <dynamicTypes>
      <add mimeType="text/*" enabled="true" />
      <add mimeType="message/*" enabled="true" />
      <add mimeType="application/javascript" enabled="true" />
      <add mimeType="*/*" enabled="false" />
    </dynamicTypes>
    <staticTypes>
      <add mimeType="text/*" enabled="true" />
      <add mimeType="message/*" enabled="true" />
      <add mimeType="application/javascript" enabled="true" />
      <add mimeType="*/*" enabled="false" />
    </staticTypes>
  </httpCompression>
</system.webServer>

Ehnancing Web Service Calls

<system.net>
  <settings>
    <!-- Makes for faster web service calls -->
    <servicePointManager expect100Continue="false" />
  </settings>
  <connectionManagement>
    <!--
      Default Value is 2, not good for our app
      KB:821268
    -->
    <add address="*" maxconnection="48" />
  </connectionManagement>
</system.net>