Recipe 14.12. Prebuilding an ASP.NET Web Site ProgrammaticallyProblemYou 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. SolutionUse the ClientBuildManager to prebuild your web site into an assembly. In order to prebuild the web site, you must specify:
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.
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'. DiscussionClientBuildManager 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 AlsoSee the "ClientBuildManager," "ClientBuildManagerParameters," "BuildManager," and "ASP.NET Web Site Precompilation" topics in the MSDN documentation. |