Pre-compilation of Applications
Dynamic compilation is targeted at reducing the number of manual steps developers have to perform while constructing a site. Pre-compilation, on the other hand, targets two issues with ASP.NET 1.x: (1) the compilation delay when first hitting a site or page, and (2) the hosting scenario, where source code must be present on the server. For intellectual property reasons this isn't always an acceptable situation.Both of these issues are tackled individually by a pre-compilation system. Part of this functionality will not be in place for the initial Technology Preview release but will be available in later betas and the final release.
In-place Pre-compilation
While ASP.NET performs extremely well, the hit taken while a site initially compiles can be quite large. To avoid this, some developers have built tools to hit every page to ensure it is compiled. In ASP.NET there is support for in-place compilation, which does just thatit compiles every file within an application root. This mechanism is done by navigating to the special URL:http://applicationDirectory/precompile.axd
| Scenario | Pages and Themes | \Resources Folder | \Code Folder | Global .asax | Personalization | Restart Application Domain? |
|---|---|---|---|---|---|---|
| Modify .aspx, .asmc, mascx | Compile as necessary | No | ||||
| Modify Global .asax | Compile | Compile | Yes | |||
| Modify source files in \Code | Compile | Compile | Compile | Compile | Yes | |
| Modify Web.config | Compile | Compile | Compile | Compile | Compile | Yes |
| Modify\Bin | Compile | Compile | Compile | Compile | Compile | Yes |
| Modify\Resources | Compile | Compile | Compile | Compile | Compile | Yes |
| Add .resx anywhere | Compile | If referenced | If referenced | No | ||
| Add source files anywhere | Compile | If referenced | No | |||
| Change themes | Late bound compile of themes only if referenced | Yes | ||||
| Personalization | Compile | Compile | Compile | Yes |
Pre-compilation for Deployment without Source
Being able to pre-compile to a target directory without the source is a great way to not only deploy ready-to-go applications but also to protect intellectual property. Pre-compilation is achieved by use of the aspnet_compiler.exe tool, whose syntax is shown below:
aspnet_compiler.exe [-m metabasePath |
-c virtualPath
[-p physicalPath]]
[targetDirectory]
where:metabasePath is the full IIS metabase path of the application.virtualPath is the virtual path of the application.physicalPath is the physical path of the application.targetDirectory is the target directory for the fully compiled application.
If the target directory is not supplied, the application is pre-compiled in place and the source is left where it is. When a target directory is specified, the target will contain no source code after the compilation. A text file is placed at the top-level directory indicating that the site has been pre-compiled. All of the source files are compiled into assemblies, and the source files have all content removed and remain simply as markers so that IIS can find the physical files.Pre-compilation does not compile static files, such as HTML files, web.config, XML files, and so on. These are just copied to the target directory. If you wish to keep the contents of HTML files from being readable in the target directory, you could rename them to .aspx so they will take part in the pre-compile. This isn't recommended, however, because it loses the benefits of IIS being able to serve HTML pages efficiently, and the performance loss may not be acceptable. Assemblies in the \Bin directory of the source application are also preserved and copied directly to the target directory.Once an application has been pre-compiled in this manner, changes (e.g., additions to the source directory) will not trigger a recompilation. Compilable files (such as .aspx files) cannot be added to the target directory once it has been generated. They must be added to the source directory and recompiled to the target.
Pre-compilation for Deployment with Source
As mentioned above, dynamic files cannot exist in a pre-compiled target directory, so it is not possible to deploy a pre-compiled application with the source intact. This is a feature that is being investigated for later releases, but for the time being the standard deployment and in-place pre-compilation is a great model to work with.
The Compilation API
The technology underlying the pre-compilation system is the Client BuildManager, which also provides an API, allowing custom tools to be built. Listing 2.6 shows how this can be achieved.
Listing 2.6 Using the Compilation API
Imports System.Web.Compilation
Public Sub BuildApplication
Dim src As String = "C:\Development\WebSites\MySite"
Dim vdir As String = "/MySite"
Dim tgt As "C:\InetPub\WWWRoot\MySite"
Dim bmgr As New ClientBuildManager(src, vdir, tgt, vbNull)
bmgr.PrecompileApp()
End Sub
• Table of ContentsA First Look at ASP.NET v. 2.0By
Publisher: Addison WesleyPub Date: October 23, 2003ISBN: 0-321-22896-0Pages: 528
"This book provides a first look at the new major release of ASP.NET. The authors have worked closely with the ASP.NET team at Microsoft to make sure that this book is authoritative, accurate, and informative. Anyone using ASP.NET will find a wealth of useful info on the next version."
-Scott Guthrie, Product Unit Manager, Web Platforms and Tools Team, Microsoft Corporation
A First Look at ASP.NET v. 2.0 systematically prepares you for Microsoft''''s ASP.NET 2.0 Web development platform, showing how to use it to reduce development time and costs, improve the performance of Web applications and services, simplify deployment and management, and extend your applications to mobile clients.
Drawing on extraordinary insider access to Microsoft''''s ASP.NET development team, three renowned ASP.NET experts cover every key platform enhancement, from Master Page templates for standardizing look and feel to improved security and data- access controls. They go far beyond already published white papers and previews, presenting detailed development techniques, best practices, and sample code that will give you a running start with ASP.NET 2.0.
This book''''s detailed coverage includes:
Streamlining database integration with "code-free" data binding and ASP.NET 2.0''''s new data source controls
Using Master Pages to improve your control over application look and feel
Strengthening security with membership providers, security server controls, cookieless forms authentication, and the new Role Manager
Simplifying personalization with ASP.NET 2.0 ThemesBuilding fast, flexible portals with Web Parts and the new Portal Framework
Delivering more effective mobile device support-with less complexity
Previewing new ASP.NET 2.0 controls and changes in existing controls
Using Microsoft''''s powerful new .NET and built-in administration tools
Whether you''''re already developing with ASP.NET, or preparing to migrate from ASP, A First Look at ASP.NET v. 2.0 gives you complete information for assessing ASP.NET 2.0-and making the most of it.