views:

23

answers:

1

I am using routing with my ASP.NET WebForms application, using the technique described by Phil Haack:

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(virtualPath, 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?

A: 

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.

marcind
I tried this, and it invokes `aspnet_compiler.exe`, rather than `csc.exe`...
Richard Ev
Even precompilation will leave .aspx stubs on the server so that ASP.NET knows how to handle them, which would cause `aspnet_compiler.exe` to run. However this shouldn't take that long. How many files do you have and how long does it take? As I said earlier, this is normal ASP.NET behavior, unless it's taking unreasonable wrong. Btw, even if you don't use precompiled sites the `aspnet_compiler.exe` will run, followed by `csc.exe`.
marcind