Internals of ASP .Net Compilation and Execution

   The ASP .Net is proved to be a worth while time spent by programmers to create applications, by providing a very rich Framework Class Library (FCL), Performance tuning, Security, Manageability etc., It has been said and written that ASP .Net follows a compiled execution model. 

Sometimes we used to get some doubts about whether writing the code inside the code behind file is faster, some times  we are not aware of how the compilation of the code happens etc., This article tries to take a brief look at some of the internals about the ASP .Net Compiled Page Rendering and Execution model along with some other related concepts, clearing such doubts.

Compilation of ASP .Net Pages:

   It is a well known fact that ASP .Net pages functional logic can be written in two ways. The ASP .Net code can either be written inside the ASPX page or it can be included as a asp_net_code.cs or vb .net file.

   When the ASPX page is embedded with the code of either C# or VB .Net, the ASP .Net run time automatically compiles the code into an assembly and loads it. If the code is kept in a separate source file either as a VB or C# file, it has to be compiled by the programmer, which will be used by the run time for further execution.

Compilation of Script Inside the aspx pages:

   When a page is created with the tags inside the .aspx page itself, the code can be written at two locations. It can either be placed inside the <script runat=”server”>…</script> tag or any where inside the page within the <%.. %> server code blocks. When the code is placed inside the script tag, it is treated to be class level code and any other code found is considered to be a part of the method that renders the web page.

   When a the aspnet_wp.exe gets a request for an aspx page which is written without a code behind class file, it generates a class file dynamically for the corresponding page and places it inside the Temporary ASP .Net Files somewhere under the tree structure of the .Net installation path. Then it compiles this into a DLL and finally deletes the class file after successful compilation. It will render the pages from the same binary DLL for any subsequent requests. If it sees any change in the time stamp of the .aspx file, it recognizes that there is some change and recompiles it again for further use.

   So ultimately the compilation is only once and all the subsequent requests are entertained only by using the compiled code/DLL.

Writing ASP .Net Apps with Code behind files:

   The compilation of these Code Behind files is usually done manually either using the csc.exe command line compiler or by using the Build feature in Microsoft Visual Studio .Net, which produces an output as a library with an extension of .DLL. Now the job of aspnet_wp.exe is very simple. It can directly execute the compiled code and return the HTML page to the web server.

Execution Flow of ASPX Pages by IIS:

   The execution of ASP .Net pages are not singly handled by the Internet Information Server or in short hand form IIS. It is taken care by the worker process aspnet_wp.exe. Whenever the IIS receives a request from a web browser or a client requesting for a page, it delegates the job to the aspnet_wp.exe process, which takes care of the subsequent jobs and finally returns the HTML page back to the IIS.

   When ASP .Net is installed, installation process creates an association for .aspx files with the aspnet_isapi.dll files. When the IIS receives a request from the clients or web browsers for an aspx page, the IIS web server hands this request over to the aspnet_isapi.dll, which in turn instantiates the aspnet_wp.exe job. This aspnet_wp.exe finalizes any unfinished jobs like run time compilation etc., as explained above and then executes the asp .net application in a new application domain. Finally the output page is generated and returned back to the web server, which in-turn sends the file over to the client.

Conclusion:

    The above is a very robust model of executing an application extending support to both the code behind and code embedded inside aspx pages. Ultimately writing the code in any place be it inside the code behind files or inside the aspx page will always be faster and will not have any performance difference between the two approaches.

     But writing the code inside a separate class file makes a clean coding approach with a separation of UI from the logic.