Precompilation in ASP.NET 2.0
 
Published: 14 Sep 2005
Unedited - Community Contributed
Abstract
To reduce response time and increase safety, ASP.NET version 2.0 introduces the ability to precompile a web application. Scott Allen outlines the new compilation models.
by Scott Allen
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 36809/ 46

Introduction

When the first request arrives at your web application there is a mind-numbing amount of work to do. The worker process starts, the runtime initializes, ASPX pages are parsed and compiled to intermediate language, methods are just-in-time compiled to native code - and the list goes on and on. If you want to cut out some of the overhead and improve the startup time of your application, then you’ll want to look at the precompile features in ASP.NET 2.0.

Although pre-compilation will give our site a performance boost, the difference in speed will only be noticeable during the first request to each folder. Perhaps a more important benefit is the new deployment option made available by the precompile - the option to deploy a site without copying any of the original source code to the server. This includes the code and markup in aspx, ascx, and master files.

In this article we will explore the benefits and caveats around pre-compilation and the new aspnet_compiler tool. There are two modes for pre-compilation: in place pre-compilation and pre-compilation for deployment. We will take a look at in place pre-compilation first.

In Place Pre-Compilation

By default, ASP.NET dynamically parses and compiles all the ASPX pages in a folder when the first request arrives for a page inside that folder. ASP.NET also needs to compile applicable files in the special folders, like App_Code, on the first request, and any code-behind files associated with ASPX and ASCX files. The runtime caches all the compilation results in order to quickly process later requests, and does not need to recompile again unless someone edits a file. This behavior gives us a great deal of flexibility, including the flexibility to change code and markup and instantly have the changes reflected in the next browser request.

The price for this flexibility is the performance hit on the first request. Some people have found their ASP.NET applications to be slow starters. These people usually work in the sales department and perform software demos in front of customers. In place pre-compilation makes the “first hit” to a web application and forces all pages and code in the application to compile.

The tool to use for pre-compilation is the aspnet_compiler executable, which you can find in the %WINDIR%\Microsoft.NET\Framework\v2.x.xxxx directory. If we have a web application in the WebSite1 virtual directory under IIS, we could use the following command line to compile the application.

Command Prompt

The –v parameter specifies that we are passing a virtual path to our web site. On servers with multiple websites you may need to use the –m parameter and specify the full IIS metabase path to the application (-m /LM/W3SVC/1/Root/WebSite1).

The pre-compiled code will end up inside of the Temporary ASP.NET File directory, just as it would when the runtime compiles files for a browser request. Inside of the bin directory for the compiled site, you’ll find the assemblies (dll files). The compiler generates special filenames to avoid naming collisions. In the shot below, the dll starting with App_Code contains the code from the App_Code directory – not too surprising. Each folder containing aspx, or ascx files will compile into a dll prefixed with App_Web. The files with a .compiled extension contain XML with information about which original source code file maps to which assembly.

Explorer

With the compiled files in place your web application should have a slightly better startup time, but a primary benefit to in place pre-compilation will be the ability to ensure the web application is error free. If you happen to modify a class or web form and leave an error in the file, the aspnet_compiler will fail and display the compiler error. The tool will also display any warnings, but warning will not stop compilation.

Pre-Compilation For Deployment

Pre-compilation for deployment creates an ‘executable’ (no source code) version of your web application. With pre-compilation for deployment you give the aspnet_compiler the path to your source code, and the path to a target directory for the compilation results, like below.

aspnet_compiler -p "C:\MyDevelopment\WebSite1" -v / C:\Staging

This command will compile the site and place the result in C:\Staging. You must still specify –v as a parameter, even though we are not using a virtual path as either a source or a destination. Instead, the compiler will use this parameter to resolve application root references (~).

The pre-compilation for deployment step will recreate your web site’s folder structure in the destination directory. All of the static files (HTML files, image files, configuration files) are copied into the folder structure exactly as they appear in the source folder hierarchy. A bin directory will appear in the target directory with all of the assemblies and .compiled files.

The target directory will contain no source code. All of the classes in the App_Code folder are now compiled into one or more assemblies in the bin directory, and no .cs or .vb files will exist in the target directory. Master page files will also compile to the bin directory and not exist. All the code and markup in ASPX, ASCX, and ASHX files, along with any associated code-behind files, will live inside of one or more assemblies in the bin directory, although these files will still exist in the target directory, they exist as nearly empty ‘marker’ files. If you open an ASPX file in a pre-compiled target directory you’ll see the following content: 

This is a marker file generated by the precompilation tool, and should not be deleted!

Note: This behavior is as of beta 2 and may change. The IIS script map for the ASPX file extension leaves the “Verify that file exists” checkbox unchecked, and the site will work without any of the ASPX files present. There is, however, a problem getting IIS to serve a default document for a directory request unless the file is present. 

Once the application finishes compiling you can FTP or XCOPY the target directory to a web server (or map a virtual directory to the target directory), and the application will be ready to run. A benefit to pre-compilation for deployment is that no one can make changes to the web application by tweaking the source code – no source code exists! In fact, you can’t even place a new ASPX file into the existing application directory structure without causing an error.

Making a change to your site will require you to make a change in the original source code, pre-compile the application again, and redeploy all files to the server. There is one caveat in this scenario, in that pre-compilation generates unique filenames for some assemblies in the bin folder, and these filenames will change each time the pre-compiler executes. The first time you run aspnet_compiler you might see App_Web_lufhs9vn.dll in the bin directory, the next time you might see App_Web_hviqdkt.dll with the same compiled code, even though no source file has changed. This means you might have unneeded dlls in your bin directory if you keep repeatedly copy files to the server without cleanup.  Use the -fixednames switch to generate repeatable assembly names.
 
For some people, the ability to update content on the web server by modifying aspx and ascx files is a needed feature. To support this scenario, the aspnet_compiler has the –u switch for an “updateable” pre-compilation. With updateable pre-compilation the ASPX, ASCX, ASHX, and MASTER files are copied to the target directory in tact - they do not become ‘marker’ files. You can deploy these files to the server and modify them without causing an error, because the ASP.NET runtime will dynamically parse and compile these files. All of the source code for code-behind files and in the App_Code folder will still be compiled into assemblies and will not need to be deployed.
 

Miscellany and Wrap Up

A few other features of the aspnet_compiler include the –d switch, which tells the compiler to generate debugging symbols for the application. Debugging symbols are required if you need line numbers in the stack traces of exceptions. The –f switch will force the compiler to overwrite a target directory, even if a precompiled application already exists there. There are also options to give compiled assemblies a strong name, and allow partially trusted callers.

In addition, you can pre-compile an application directly from Visual Studio 2005 using the Build -> Publish menu item. The Publish Web Site dialog will let you select an http, ftp, or local file system destination as the target.

The precompiler also supports strong naming with the -keyfile, -keycontainer, and -delaysign switches. To allow partially trusted callers, use the -aptca switch.

Although performance is often touted as a benefit of pre-compilation, I tend to think of pre-compilation as a safety feature. Safety in knowing all of the code in a web application compiles without an error, and safety in keeping your source code out of a shared host environment.



User Comments

Title: Issue releated to user controls   
Name: Hitendra Patel
Date: 2008-06-09 5:14:03 AM
Comment:
Hi,

Article is good but still the information which I required, that is not here.

I have searched on any sites but nowhere I get.

Issue is , I have may usercontrols which are having custom event and also contain other user controls (as a child control). When I'm using per-compilation option, its not able to find out dependent controls and that makes compilation fail.

I could able to build the project in VS2005 and site is also working with error but precompilation is not able to find out proper reference of used usercontrol or customer control.

FYI, I have paste error here.
cx(10): error CS0117: 'ASP.usercontrols_leftpanel_ascx' does not contain a definition for 'ReportNodeClicked'

actually this control is loaded by one of the aspx page and same event is written in codebehind file of that page.

Pl help me to resolve this issue if any one can.

Regards
Title: Bad feature   
Name: RM
Date: 2007-05-18 11:23:54 AM
Comment:
While it might be useful in some situations, this is a very bad feature to have enabled by default.

I've been trying for eons to find out how I can go back to the normal way of publishing so I can update some HTML in the aspx files without having to recompile and republish the WHOLE site, which is very counter productive.
Title: partial deployment   
Name: kiht
Date: 2007-03-22 3:06:58 AM
Comment:
Hi,
So if I have to update just 1 aspx.cs file, do I need to precompile and copy the whole bin folder on production?
Or is it possible just to deploy one particular dll. Would be helpful if you let me know.
Title: Very good   
Name: Bas
Date: 2006-11-13 3:27:09 PM
Comment:
I was pretty frustrated by the initial slowness of asp.net 2.0, but this precompiling solves it all!

Thanks for sharing!
Title: Great!!   
Name: Diego Moreno
Date: 2006-07-17 1:46:53 PM
Comment:
Nice job!!, i found this information very usefull, nice examples too
Title: Thank the stars   
Name: Eben Roux
Date: 2006-05-06 2:50:18 AM
Comment:
Hi Scott,

I have been updating my sites from ASP 1.1 to 2.0 and have started thinking that the version 2 implementation may have been a slight strategic error on Microsoft's part... but as usual it boils down to knowing the tool. I was struggling to understand how things should be compiled and where they should go. Even tried the WAP stuff and that gave me some more headaches.

Your simple article has really helped. Thanks for that.

Regards,
Eben
Title: Great job   
Name: Chris Holmes
Date: 2006-01-05 1:21:22 PM
Comment:
Thanks for the article Scott. This was just what I needed.

We just converted a small internal web app from ASP 1.1 to ASP 2.0. I was shocked to find out that I had to copy the actual codeFiles (.cs) to my web directory in order to get the applicationt to run properly. I don't know about anyone else, but the idea that my raw source files are located on the webserver scared me to death.

Fortunately, your article explained the other methods for deployment in clear detail. We precompiled our site and deployed it with VS.NET 2005's GUI version of the command-line tool.

So thanks for posting this!
Title: Thanks   
Name: Scott Allen
Date: 2005-12-21 4:44:16 PM
Comment:
Thanks, Ahmet.
Title: very useful info   
Name: Ahmet KUCUK
Date: 2005-12-19 4:20:42 PM
Comment:
especialy Asp.net 2.0 express edition doesnt have gui tool
for compiled publishing.
so that compilation commands and techniques is very useful information.
Thanks.
Title: Re: Can we also deploy user controls...   
Name: Scott Allen
Date: 2005-11-01 11:38:57 PM
Comment:
Deployable user controls are possible in ASP.NET 2.0. Take a look at this article on my blog: http://odetocode.com/Blogs/scott/archive/2005/10/06/2326.aspx, and also check out the comments where I have some other links.
Title: Can we also deploy user controls (.ascx) files in this way.   
Name: Umut Tezduyar
Date: 2005-11-01 5:30:04 PM
Comment:
For example, i have written a custom control, that has .ascx extension and i want to publish this control as a reusable component for other developers. Can precompile also work in this scenario.
Title: Nice work   
Name: Kenneth
Date: 2005-10-24 7:45:07 PM
Comment:
very touchy and helpful article for webmasters who deploy their code in shared host.

Would be nice to see some in-action examples.
Title: Great!   
Name: Kalyan Kumar
Date: 2005-09-24 10:45:05 AM
Comment:
This article clearly explains about Precompilation techniques in ASP.NET 2.0.

Good Work!Keep it up!!!!
Title: Thanks   
Name: Murugan JK
Date: 2005-09-24 10:43:12 AM
Comment:
Thanks for this solution. It's an outstanding compilation techniques.

Keep it up!
Title: Precompilation in ASP.NET 2.0   
Name: Adams
Date: 2005-09-24 10:41:10 AM
Comment:
Great Article!, i need to clarify, what about our previous deployment methodologies?
Title: Precompilation in ASP.NET 2.0   
Name: sateesh
Date: 2005-09-22 3:33:34 PM
Comment:
Nice article...fantasic!!
Title: excellent   
Name: Stefan
Date: 2005-09-20 12:15:58 AM
Comment:
Nice Article!

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-25 4:22:41 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search