Asp.NET有许多秘密,当你了解了这些秘密后,可以使得你的ASP.NET应用程序达到极大的性能提升。举例来说,在使用Membership和profile进行Authentication和authorization时,可以通过简单的修改Membership和profile以提高Authentication和authorization的性能;ASP.NET Http 管道通过调整以避免执行不必要的HttpModules;不仅如此,浏览器端的页面缓存可以为重复浏览节省大量的下载时间;按需UI加载可以使网站更快、更平滑;最后,t适当的应用CDN(Cotent Delivery Networks)和应用Http Cache Header可以使网站更加快速。下面就让我们来分别讨论这些技巧,以提高我们ASP.NET网站的性能。
用户每次请求网站时,都会有许多ASP.NET默认HttpModules去中断请求并进行一些处理,例如SessisonStateModule会中断每次的请求,解析session cookie信息并加载的适当的Session给HttpContent.但是这些默认的HttpModules并不都是必须的,例如如果你没有应用Forms Authentication来进行用户验证,那就不需要FormsAuthentication Module。
默认的Modules是在machine.config文件(该文件的位置为:$WINDOWS$\Microsoft.NET\Framework\$VERSION$\CONFIGdirectory))定义的,其定义如下:.
<httpModules>
<addname="OutputCache"type="System.Web.Caching.OutputCacheModule"/>
<addname="Session"type="System.Web.SessionState.SessionStateModule"/>
<addname="WindowsAuthentication"
type="System.Web.Security.WindowsAuthenticationModule"/>
<addname="FormsAuthentication"
type="System.Web.Security.FormsAuthenticationModule"/>
<addname="PassportAuthentication"
type="System.Web.Security.PassportAuthenticationModule"/>
<addname="UrlAuthorization"type="System.Web.Security.UrlAuthorizationModule"/>
<addname="FileAuthorization"type="System.Web.Security.FileAuthorizationModule"/>
<addname="ErrorHandlerModule"type="System.Web.Mobile.ErrorHandlerModule,
System.Web.Mobile, Version=1.0.5000.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
</httpModules>
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }你要以通过在web.config中添加<remove>节点来移除不必要的httpModules,比如如果一个网站使用的是基于数据库的forms authentication验证并且没有使用任何Session则可以在web.config中添加如下节点:
<httpModules>
<!-- Remove unnecessary Http Modules for faster pipeline -->
<removename="Session"/>
<removename="WindowsAuthentication"/>
<removename="PassportAuthentication"/>
<removename="AnonymousIdentification"/>
<removename="UrlAuthorization"/>
<removename="FileAuthorization"/>
</httpModules>
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }2 ASP.NET进程最优化配置
ASP.NET进程模块配置了许多进程级别的属性,比如Asp.NET可以使用多少线程;超时多长时间后阻塞一个线程;在IO线程完成工作时,应该由多少个请求保持等待。默认的配置有许多限制,但是现在硬件变得越来越便宜,双核处理和GB的内存变成了标准配置,所以我们要以调整默认配置,让Asp.net进程使用更多的系统资源。
默认情况下,mechine.config文件中是这样配置asp.net进程的。
<system.web>
<processModelautoConfig="true"/>
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }
我们可以调整默认配置,为每个属性指定不同的值,以定制asp.net线程的工作模型,例如:
<processModel
enable="true"
timeout="Infinite"
idleTimeout="Infinite"
shutdownTimeout="00:00:05"
requestLimit="Infinite"
requestQueueLimit="5000"
restartQueueLimit="10"
memoryLimit="60"
webGarden="false"
cpuMask="0xffffffff"
userName="machine"
password="AutoGenerate"
logLevel="Errors"
clientConnectedCheck="00:00:05"
comAuthenticationLevel="Connect"
comImpersonationLevel="Impersonate"
responseDeadlockInterval="00:03:00"
responseRestartDeadlockInterval="00:03:00"
autoConfig="false"
maxWorkerThreads="100"
maxIoThreads="100"
minWorkerThreads="40"
minIoThreads="30"
serverErrorMessageFile=""
pingFrequency="Infinite"
pingTimeout="Infinite"
asyncOption="20"