Tuesday, 29 March 2011

Understanding ASP.NET Dynamic Compilation

When request processes we don’t know, how processes exactly works or compiles, recompilation process and how it will give compilation output. Let’s start with how it will exactly work.
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.
ASP.NET dynamic compilation enables you to modify your source code without having to explicitly compile your code before you deploy 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 <process Model> section has been changed.
-          Compiling on First Request
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.
-          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.
-          Compilation Dependencies
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 Global Resources folder, the App Web Resources 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 Local Resources 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.
-          Compilation Output

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.
Following example which shows steps of compilation in easy way
  1. Machine boots.
  2. Request for MyPage.aspx comes in (first time).
  3. MyPage.aspx compiles to an assembly* (we'll call it MyPage.dll* ).
  4. The portions of MyPage.dll that are needed for execution are JITed* to memory.*
  5. Page execution completes. The JITed code stays in memory.
  6. A new request for MyPage.aspx comes in. The page hasn't changed. The JITed code that is still in memory runs.
  7. The source for MyPage.aspx changes.
  8. A new request for MyPage.aspx comes in. MyPage.aspx compiles to an assembly, JITs, and executes. The JITed code stays in memory.
  9. A new request for MyPage.aspx comes in. The page hasn't changed. The JITed code that is still in memory runs.
  10. IIS is stopped and started.
  11. A new request for MyPage.aspx comes in. The page hasn't changed. Because IIS was stopped and started, the JITed code is no longer in memory. The existing MyPage.dll JITs and executes. This new JITed code stays in memory.

No comments:

Post a Comment