Recipe14.12.Prebuilding an ASP.NET Web Site Programmatically


Recipe 14.12. Prebuilding an ASP.NET Web Site Programmatically

Problem

You want to prebuild your web site to avoid compilation delays and to avoid the hosting scenario in which source code needs to be on the server.

Solution

Use the ClientBuildManager to prebuild your web site into an assembly. In order to prebuild the web site, you must specify:

  • The virtual directory for the web application

  • The physical path to the web application directory

  • The location where you want to build the web application

  • Flags that help control the compilation

To prebuild the web application in the sample code for the book, first retrieve the directory where the web application is located, then provide a virtual directory name and a location for the web application to build to:

 // Get the path to the web app shipping with this code… string cscbWebPath = GetWebAppPath(); // Make sure we have a web path. if(cscbWebPath.Length>0) {     string appVirtualDir = @"CSCBWeb";     string appPhysicalSourceDir = cscbWebPath;     // Make the target an adjacent directory as it cannot be in the same tree     // or the build manager screams…     string appPhysicalTargetDir = Path.GetDirectoryName(cscbWebPath) + @"\BuildCSCB"; 

Next, set up the flags for the compile using the PrecompilationFlags enumeration. The PrecompilationFlags values are listed in Table 14-2.

Table 14-2. PrecompilationFlags enumeration values

Flag value

Purpose

AllowPartiallyTrustedCallers

Add the APTC attribute to the built assembly.

Clean

Remove any existing compiled image.

CodeAnalysis

Build for code analysis.

Default

Use the default compile options.

DelaySign

DelaySign the assembly.

FixedNames

Assembly generated with fixed names for pages. No batch compilation is performed, just individual compilation.

ForceDebug

Ensure that the assembly is compiled for Debug.

OverwriteTarget

The target assembly should be overwritten if it exists.

Updateable

Insure the assembly is updateable.


To build a debug image and make sure it is created successfully if the compilation is good, the ForceDebug and OverwriteTarget flags are used:

     PrecompilationFlags flags = PrecompilationFlags.ForceDebug |                       PrecompilationFlags.OverwriteTarget; 

The PrecompilationFlags are then stored in a new instance of the ClientBuildManagerParameter class, and the ClientBuildManager is created with the parameters that have been set up for it. To accomplish the prebuild, the PrecompileApplication method is called. Notice that there is an instance of a class called MyClientBuildManagerCallback that is passed to the PrecompileApplication method.

     ClientBuildManagerParameter cbmp = new ClientBuildManagerParameter();     cbmp.PrecompilationFlags = flags;     ClientBuildManager cbm =                  new ClientBuildManager(appVirtualDir,                              appPhysicalSourceDir,                              appPhysicalTargetDir,                                                  cbmp);     MyClientBuildManagerCallback myCallback = new MyClientBuildManagerCallback();     cbm.PrecompileApplication(myCallback); } 

The MyClientBuildManagerCallback class is derived from the ClientBuildManagerCallback class and allows the code to receive notifications during the compilation of the web application. Compiler errors, parsing errors, and progress notifications are all available. In the MyClientBuildManagerCallback class, they are all implemented to write to the debug stream and the console.

 public class MyClientBuildManagerCallback : ClientBuildManagerCallback {     public MyClientBuildManagerCallback()         : base()     {     }     public override void ReportCompilerError(CompilerError error)     {         string msg = "Report Compiler Error: " + error.ToString();         Debug.WriteLine(msg);         Console.WriteLine(msg);     }     public override void ReportParseError(ParserError error)     {         string msg = "Report Parse Error: " + error.ToString();         Debug.WriteLine(msg);         Console.WriteLine(msg);     }     public override void ReportProgress(string message)     {         string msg = "Report Progress: " + message;         Debug.WriteLine(msg);         Console.WriteLine(msg);     } } 

The output from a successful compilation of the CSCB web site looks like this:

 Report Progress: Building directory '/CSCBWeb/App_Data'. Report Progress: Building directory '/CSCBWeb/Role_Database'. Report Progress: Building directory '/CSCBWeb'. 

Discussion

ClientBuildManager is actually a thin wrapper around the BuildManager class, which does most of the heavy lifting of the compilation. ClientBuildManager makes it more straightforward to ensure that all the important parts of the web application are addressed, while BuildManager gives a bit more fine-grained control. The ClientBuildManager also allows for subscribing to appdomain notification events such as start, shutdown, and unload, allowing for error handling in the event that the appdomain is going away during a prebuild.

To prebuild applications in ASP.NET 2.0 without resorting to the ClientBuildManager, an HTTP request can be posted to the web site in the format of http://server/webapp/precompile.axd. The precompile.axd "document" triggers an ASP.NET HttpHandler for this that will prebuild the web site for you. This is handled by the aspnet_compiler.exe module that essentially wraps the ClientBuildManager functionality.

See Also

See the "ClientBuildManager," "ClientBuildManagerParameters," "BuildManager," and "ASP.NET Web Site Precompilation" topics in the MSDN documentation.



C# Cookbook
Secure Programming Cookbook for C and C++: Recipes for Cryptography, Authentication, Input Validation & More
ISBN: 0596003943
EAN: 2147483647
Year: 2004
Pages: 424

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net