Using routing with webforms - CreateInstanceFromVirtualPath sometimes very slow
I am using routing with my ASP.NET WebForms application, using the technique described by Phil Haack:
- http://haacked.com/archive/2008/03/11/using-routing-with-webforms.aspx
This works well most of the time, however on on occasion the first call to System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath
is takes tens of seconds to return.
This happens in the following method:
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
LapTimer lapTimer = new LapTimer();
string virtualPath = this.GetSubstitutedVirtualPath(requestContext, lapTimer);
if (this.CheckPhysicalUrlAccess && !UrlAuthorizationModule.CheckUrlAccessForPrincipal(virtualPath, requestContext.HttpContext.User, requestContext.HttpContext.Request.HttpMethod))
throw new SecurityException();
IHttpHandler page = BuildManager.CreateInstanceFromVirtualPath(virtua开发者_StackOverflow社区lPath, typeof(Page)) as IHttpHandler;
if (page != null)
{
//Pages that don't implement IRoutablePage won't have the RequestContext
//available to them. Can't generate outgoing routing URLs without that context.
var routablePage = page as IRoutablePage;
if (routablePage != null)
routablePage.RequestContext = requestContext;
}
return page;
}
At the same time as this I notice (using Task Manager) that a process called csc.exe, the C# compiler, is taking up 10%-50% of my CPU.
Can anyone suggest why this would be happening?
Your application is using runtime compilation of views. While your business logic, codebehind etc (basically any .cs file) gets compiled by Visual Studio, your views (*.aspx, *.ascx, *.Master) are compiled by the asp.net runtime when a given view is first requested (i.e. the BuildManager is asked for an object that corresponds to a given virtual path). It might take some time because views might be compiled in batches (e.g. all views in a single folder).
A view will be recompiled if you change it. Also all view compilations will be invalidated if the App Domain recycles (which can happen if you make changes to web.config, global.asax, etc).
All this is normal behavior in ASP.NET. If you find that this is unacceptable in your scenarios you can use precompiled applications. This will provide you with app startup perf benefits at the cost of being able to easily tweak the markup of your site withouth having to recompile everything.
精彩评论