ASP .NET Pre-compilation

ASP .NET 2.0 comes with a handy feature called ASP pre-compilation. It is described in detail on a number of pages:

http://www.odetocode.com/Articles/417.aspx
http://msdn2.microsoft.com/en-us/library/ms228015.aspx

There is even an add-on for Visual Studio 2005 that can do the pre-compilation for Web Application Projects:

http://msdn2.microsoft.com/en-us/library/ms228015.aspx

Pre-compilation

This is all well and good but what if you have a non-trivial site with multiple Visual Studio projects and multiple virtual directories? The good news is that it is possible to pre-compile any complex project for deployment, the bad news is that the of doing so is not as straight forward and as flexible as one hopes it is.

The scenario described in this blog probably applies if you have projects that are converted from Visual Studio 2003. I am going to use a sample project with three projects - Main, FrameworkControls and MyControls. Main project contains pages and controls relevant to this application, Controls contains some controls and code that you’ve written and FrameworkControls is a project that is shared with another team and contains some web user controls and some code. Let’s also assume that Main uses controls from MyControls which in turn uses controls from FrameworkControls.

Below is a map of the web site that displays how the virtual directories are mapped.

Project directory………..Virtual directory

Main……………/
MyControls  
+www…………controls           
+src
FrameworkControls
+www…………framework             
+src

Let’s now try to pre-compile it.

aspnet_compiler -p c:\Precompilation\Main c:\Precompilation\deployed

The above obviously wouldn’t work as the compiler doesn’t know how this physical folder is mapped to a virtual folder in the IIS. In fact it will tell you to specify either -m(metabse path) or -v(virtual path) with the -p command. Let’s try to specify the virtual path:

aspnet_compiler -f -p c:\Precompilation\Main -v / c:\Precompilation\deployed

This fails with this rather interesting error:

/Default.aspx(2): error ASPPARSE: The file ‘/controls/FramedButtonLabel.ascx’ does not exist.
/Default.aspx(15): error ASPPARSE: Unknown server tag ‘ctrls:ButtonLabel’.

The ASP compiler now knows where the root folder is mounted, however since the physical folder is specified, it will only look within that folder and hence not find other virtual folders. This is fixed by running the following command:

aspnet_compiler -f -v / c:\Precompilation\deployed

If there are no errors in the pages the site will compile. Note that the output of the pre-compilation is the contents of the Main folder with the folder controls and framework folders inside it, in other words, as IIS “sees” it. Alternatively you can specify the metabase path:

aspnet_compiler -f -m /LM/W3SVC/1/Root/ c:\Precompilation\deployed

The metabase path might come in handy when using the web deployment projects.

Unfortunately with this site layout it’s impossible to compile individual projects. So you can’t compile controls or framework because they don’t have the bin folder. So any assemblies that those two projects reference will not be found.

aspnet_compiler -f -v /controls c:\Precompilation\deployed

You will see this kind of error executing the above command:

/controls/FramedButtonLabel.ascx(1): error ASPPARSE: Could not load type ‘MyControls.www.FramedButtonLabel’.
/controls/FramedButtonLabel.ascx(2): error ASPPARSE: The file ‘/controls/framework/ButtonLabel.ascx’ does not exist.
/controls/FramedButtonLabel.ascx(5): error ASPPARSE: Unknown server tag ‘fm:ButtonLabel’.

Excluding projects from pre-compilation

A neat little trick that can be done (although the usefulness of it is near zero) is an ability to exclude projects from pre-compilation. You do that by creating an application on the virtual directory you want to exclude(under the “Home Directory” tab in the virtual directory’s properties). So if we create an application for, say, controls directory we will exclude it from pre-compilation. Running the pre-compilation again for the root folder yields the contents of the Main folder with only framework directory inside it.

blog comments powered by Disqus