Understanding ASP.NET Dynamic Compilation
In order for your Web application to service requests, ASP.NET must first parse and compile the code of your Web application into one or more assemblies. When the code is compiled, it is translated into a language-independent and CPU-independent representation called Microsoft Intermediate Language (MSIL). At run time, MSIL runs in the context of the .NET Framework, which translates MSIL into CPU-specific instructions for the processor on the computer running the application.
Compiling on First Request
By default, ASP.NET Web pages and code files are compiled dynamically when users first request a resource, such as an ASP.NET page (.aspx file), from a Web site. After pages and code files have been compiled the first time, the compiled resources are cached, so that subsequent requests to the same page are extremely efficient.
ASP.NET supports the dynamic compilation of ASP.NET pages (.aspx files), ASP.NET Web services (.asmx files), ASP.NET HTTP handlers (.ashx files) and ASP.NET application files (Global.asax), as well as other files, such as source code and class files. For more information about ASP.NET file types, see. For more information about the ASP.NET compilation process, see the "Compilation Life Cycle" section of .
Recompiling on Change
Any changes to a dynamically compiled file will automatically invalidate the file's cached compiled assembly and trigger recompilation of all affected resources. The next time a request to the code is made, ASP.NET recognizes that the code has changed and recompiles the affected resources of the Web application. This system enables you to quickly develop applications with a minimum of compilation processing overhead. (Note that depending on the change to the resources, the result can range from recompiling a single page to recompiling the entire Web site.)
When the first request is made to an application, ASP.NET compiles files in a specific order. The first items to be compiled are referred to as the top-level items. After the first request, the top-level items are recompiled only if a dependency changes.
Top-level items include the App_GlobalResources folder, the App_WebResources folder, profile properties, the App_Code folder, and the Global.asax file. After the top-level items are compiled, ASP.NET compiles additional items. These items include the App_LocalResources folder, individual ASP.NET pages (.aspx files), ASP.NET user controls (.ascx files), ASP.NET HTTP Handlers (.ashx files), and ASP.NET HTTP modules (.asmx files), as well as themes, master pages, and other source files.
For more information, seeand the "Compilation Life Cycle" section of .
When your code is compiled, the resulting assemblies are cached in a folder on the server. This folder requires appropriate permissions so that your code compiles and runs correctly. You can configure both the compilation folder location and the permissions under which your code compiles and operates.
Compilation Folder Location
By default, when you compile a Web application the compiled code is placed in the Temporary ASP.NET Files folder. This folder is a subdirectory of the location where you installed the .NET framework. Typically, the location is the following:
%SystemRoot%\Microsoft.NET\Framework\versionNumber\Temporary ASP.NET Files
Compilation Folder Required Permissions
The .NET installation process creates the Temporary ASP.NET Files folder and assigns access permissions to the ASP.NET local user account, which has the high-trust permissions needed to access your compiled code. If you modify your configuration or account settings, you must make sure that the account you use has high-trust permissions to the Temporary ASP.NET Files folder. For additional details, see.
Compilation Folder Configurability
ASP.NET creates a discrete subfolder under the Temporary ASP.NET File folder for each application. You can configure the root location using the tempDirectory attribute of the compilation section of the configuration file. This optional attribute enables you to specify the directory to use for temporary file storage during compilation. The default is an empty string (""). In the case of an empty string, and if the current process has the required access permissions, the files are stored in the following directory:
%FrameworkInstallLocation%\Temporary ASP.NET Files
For more information, seeand the property of the .
Multiple Language Support
ASP.NET 2.0 supports multiple programming languages in the same Web application. In the App_Code directory, you can specify a subfolder for each language, such as C# and Visual Basic. ASP.NET will create a separate assembly for each subfolder. For more information, seeand .
Dynamic Compilation Advantages and Disadvantages
ASP.NET dynamic compilation enables you to modify your source code without having to explicitly compile your code before deploying your Web application. If you modify a source file, ASP.NET automatically recompiles the file and updates all linked resources. The IIS server does not have to be restarted for the changes to take effect unless the <processModel> section has been changed. Additionally, you can extend the ASP.NET build system by creating custom build providers for new file types that are called during compilation. The dynamic compilation benefits of the ASP.NET build system are backward compatible with older ASP.NET application structures and types as well.
There are certain capabilities that dynamic compilation does not offer. Dynamic compilation can mean slower initial response time for users, because pages and code files must be compiled the first time they are requested. This may be an issue particularly on large sites that are updated frequently. Dynamic compilation does not offer a means to identify compile-time bugs before users access a site. Also, dynamic compilation does not provide the ability to create a compiled version of the site that can be deployed to a production server without source code. If any of these issues are concerns for your Web applications, you can precompile your Web site. For details information, see.