Boost ASP.NET Performance with Precompilation

he latest version of ASP.NET, version 2.0, supports several new and exciting features that promise to enhance developer productivity, administration and management, extensibility, and performance. One of these features is precompilation, which either developers or administrators can use to precompile ASP.NET applications before they are deployed. Moreover, the new precompilation feature can detect and provide warnings about any compilation failure issues, and lets you deploy applications without the need to store the source code on the deployment server. Precompilation can both reduce application response time and improve performance. This article explains how to use the new feature effectively.

Why Precompilation?
Dynamic compilation model of ASP.NET Whidbey refers to the ability of ASP.NET to compile the application’s code dynamically at runtime. Because the ASP.NET framework senses changes to source pages, this capability means you can change your web page, and then reload it using the same request from a browser to see the page changes without having to recompile your entire web site. That’s a great feature during development, but not necessarily after deployment, where you’re less likely to be changing pages constantly. Using the default model, though, ASP.NET still compiles pages the first time they get requested, even on the deployment server, and even if the pages haven’t changed since the last time that application was loaded. But what if you could precompile your entire web site before deployment? This is where ASP.NET 2.0’s precompilation feature fits in.

?
Figure 1. Aspnet_compiler Syntax: You can see a list of aspnet_compiler.exe options by adding the “/?” option to the command line.

Precompilation in ASP.NET Whidbey is available in two modes.

  • In-place precompilation
  • Precompilation for deployment

In Place Precompilation
This option enables you to precompile your entire project. In essence, it compiles each and every page of your application and displays any compilation errors that occur. The tool you use to precompile applications is called aspnet_compiler and it resides in the %WINDIR%Microsoft.NETFrameworkv2.x.xxxx directory. Figure 1 displays help on all the available options of this tool when invoked with the “/?” parameter from the command line.

While Figure 1 lists many options, the basic syntax for using this tool is as follows.

   aspnet_compiler -v / -p  

For example, you would use the following statement at the command line to precompile a “TestWebSite” virtual directory using the aspnet_compiler precompilation tool.

   aspnet_compiler -v /TestWebSite
?
Figure 2. Temporary ASP.NET Files: By default, precompiled libraries get stored in the “Temporary ASP.NET Files” folder.

Note that the “-v” option shown above specifies a virtual directory. By default, the precompiled library created above gets stored in the Temporary ASP.NET Files directory as shown in Figure 2.

Precompilation for Deployment
This feature allows you to compile your project prior to its deployment. You can use this option to compile your projects into dynamically linked libraries and copy those to the deployment server. You no longer need to store your application’s source code there.

?
Figure 3. Precompilation for Deployment: Using a command line like the one shown, you can precompile your project into DLL files for deployment on the server.

With this option, you have to specify the path to your source code and the path to the target directory where you need the compiled library to be stored. Figure 3 illustrates how to precompile a web site called “TestWebSite” using this tool and store the compiled library in the specified directory.

The statement shown in Figure 3 precompiles the specified web site and stores the compiled library in the “C:TestWebSiteDeployment” directory. The “-p” option precedes the name of the source web site.

You can use the “-f” option of this tool to overwrite the content of the target directory if it already contains a compiled library with the same name (see Figure 4)

You can also precompile your web site using the “Publish Web Site” option of VS.NET 2005 as shown in Figure 5.

?
Figure 4. Overwriting Existing Files: Use the “-f’ option to overwrite existing library files.
?
Figure 5. Precompiling in Visual Studio: You don’t have to use the command-line tool; the Publish Web Site dialog lets you precompile a web site.

If you check the option “Allow this precompiled site to be updatable” in the Publish Web Site dialog as shown in Figure 5, you’ll find that the initial load time for web pages in the site will still be expensive. That’s because when you specify that option, the ASP.NET runtime environment still has to compile the web page dynamically the first time it is requested. Therefore, unless you need to update your web pages frequently, precompile your web site with the option unchecked to minimize the initial load time.

Apart from using the precompilation feature, my previous article on ASP.NET performance shows other ways to reduce the load time of your .aspx web pages the first time they are requested.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Related Posts