Microsoft.NET

……………………………………………….Expertise in .NET Technologies

The ASP.NET Compilation Model

Posted by Ravi Varma Thumati on July 16, 2009

One of the most important improvements Microsoft has made to the ASP development environment is to build the Web request handling framework out of classes. Pushing request processing into a class-based architecture allows for a Web-handling framework that’s compiled. When ASP.NET pages are first accessed, they are compiled into assemblies.

This is advantageous because subsequent access loads the page directly from the assembly. Whereas classic ASP interpreted the same script code over and over, ASP.NET applications are compiled into .NET assemblies and ultimately perform better and are safer. Because the code is compiled, it runs more quickly since it doesn’t have to be interpreted. In addition, the managed runtime is a type-safe environment; you won’t see the same sorts of errors and anomalies that you’d encounter in a scripting environment (as was the case for classic ASP).

In addition, compiling the Web request framework allows for more robust and consistent debugging. Whenever you run an ASP.NET application from Visual Studio, you can debug it as though it were a normal desktop application.

ASP.NET compiles .aspx fi les automatically. To get an .aspx page to compile, you simply need to surf to the .aspx fi le containing the code. When you do so, ASP.NET compiles the page into a class. However, you won’t see that assembly containing the class anywhere near your virtual directory. ASP.NET copies the resulting assemblies to a temporary directory.

The .NET versions of Microsoft Visual Studio have always included a tool named Intermediate Language Disassembler (ILDASM) that uses refl ection to reverse compile an assembly so you may view its contents. The result is an easily negotiated tree view you may use to drill down to the contents of the assembly. Right now, that’s the important thing. (If you want to peer any more deeply into the assembly and see the actual Intermediate Language, ILDASM will show you that as well.)

Viewing the ASP.NET assemblies

Here’s how to view the assemblies generated by ASP.NET.

  • To run ILDASM, open the Visual Studio .NET 2008 command prompt and type ILDASM.
  • Select File, Open.
  • Find the assembly compiled by the ASP.NET runtime. Go to C:\WINDOWS\Microsoft .NET\Framework\v2.0.50727\Temporary ASP.NET Files\aspnetstepbystep\. The subdirectory is named v2.0.50727 at the time of this writing. The fi nal subdirectory may be slightly different. You’ll see some oddly named directories underneath. For example, on my machine, the subdirectory names generated by ASP.NET are 110a3860 and 9bf9cc39. The directory name(s) will most likely be different on your machine. There’s no easy way to figure out which directories have the code that just executed (though looking at the dates and times of the file creation may help), so you’ll need to drill down into the directories until you unearth some DLL files. Depending on how many times you’ve run the application, you may see several files. Open the fi les one at a time until ILDASM displays something similar to what’s shown in Figure 2-4.

ASP.NET has used this temporary directory strategy since version 1.0. The reason ASP.NET copies these files to a temporary directory are to solve a long-standing problem that plagued classic ASP. Classic ASP Web sites often depended on COM objects to do complex operations such as database lookups and transactions. When you deploy a classic ASP site and clients begin accessing it, those files become locked. Of course, that’s not really a problem—until you decide to upgrade or modify part of the Web site.

compiler

Classic ASP locked files during execution, meaning you couldn’t copy new files into the virtual directory without shutting down the Web site. For many Web deployment scenarios, this is a bad option. Because ASP.NET copies the files and the components to the temporary directory and runs them from there, they’re not locked. When it is time to update a component, simply copy the new assembly into the virtual directory. You can do that because it’s not locked.

Coding Options

In addition to supporting inline code (that is, including executable code directly inside a server side script block), modern ASP.NET offers two other distinct options for managing code: ASP.NET 1.x code behind, and modern ASP.NET code beside. ASP.NET supports code behind for backward compatibility. Code beside is the style employed by Visual Studio 2008. Let’s look at these.

ASP.NET 1.x Style

ASP.NET continues to support ASP.NET 1.x style code behind. This may be important to understand if you ever run into any legacy code from that era. Using the code-behind directives in the ASPX file, you provide the code to run behind the page in a separate class and use the Page directive to tell ASP.NET which class to apply to the page. Then you tell ASP.NET the name of the file containing the source code for the class.

For example, imagine this code is placed in a file named HelloWorld4Code.cs:

using System.Web;

public class HelloWorld4Code : System.Web.UI.Page

{

public void ShowLineage()

{

Response.Write(“Check out the family tree: <br/> <br/>”);

Response.Write(this.GetType().ToString());

Response.Write(” which derives from: <br/> “);

Response.Write(this.GetType().BaseType.ToString());

Response.Write(” which derives from: <br/> “);

Response.Write(this.GetType().BaseType.BaseType.ToString());

Response.Write(” which derives from: <br/> “);

Response.Write(

this.GetType().BaseType.BaseType.BaseType.ToString());

Response.Write(” which derives from: <br/> “);

Response.Write(

this.GetType().BaseType.BaseType.BaseType.BaseType.ToString());

}

}

An ASP.NET page that uses the HelloWorld4Code class to drive the page might then look like this:

<%@ Page Inherits=”HelloWorld4Code” Src=”HelloWorld4Code.cs” Debug=”true” %>

<html>

<body>

<h1>Hello World!!!</h1>

<%

this.ShowLineage();

%>

</body>

</html>

With the ASP.NET 1.x style of code behind, ASP.NET sees the Src attribute in the directives and compiles that file. ASP.NET reads the Inherits attribute to figure out how to base the class that runs the page. In the example above, ASP.NET uses the HelloWorld4Code class to drive the page.

By using the Src attribute, you tell the ASP.NET runtime to compile the file named by the Src attribute value. The ASP.NET runtime will compile it into the temporary directory. Alternatively, you may also pre-compile the file into an assembly containing the HelloWorld4Code class. For this to work, the precompiled assembly must appear in the bin directory of your virtual directory.  If you pre-compile the page class and put the assembly in the bin directory, you don’t even need to mention the source code file. In the absence of a Src attribute, the ASP.NET runtime will search the assemblies in the bin directory looking for the class specified in the Inherits attribute.

Modern ASP.NET Style

The other coding option for ASP.NET is new starting with version 2.0. This model is sometimes referred to as code beside. Consider the following ASP.NET page:

<%@ Page CodeFile=”HelloWorld5Code.cs” Inherits=”HelloWorld5Code” %>

<html>

<body>

<h1>Hello World!!!</h1>

<%

// This block will execute in the Render_Control method

ShowLineage();

%>

</body>

</html>

It references the code found in the HelloWorld5Code.cs file:

using System.Web;

public partial class HelloWorld5Code : System.Web.UI.Page

{

public void ShowLineage()

{

Response.Write(“Check out the family tree: <br/> <br/>”);

Response.Write(this.GetType().ToString());

Response.Write(” which derives from: <br/> “);

Response.Write(this.GetType().BaseType.ToString());

Response.Write(” which derives from: <br/> “);

Response.Write(this.GetType().BaseType.BaseType.ToString());

Response.Write(” which derives from: <br/> “);

Response.Write(

this.GetType().BaseType.BaseType.BaseType.ToString());

Response.Write(” which derives from: <br/> “);

Response.Write(

this.GetType().BaseType.BaseType.BaseType.BaseType.ToString());

}

}

In this case, ASP.NET looks to the CodeFile directive to fi gure out what code to compile. ASP.NET expects to fi nd a partial class to implement the page’s logic. Partial classes let you split the defi nition of a type (class, struct, or interface) between multiple source fi les, with a portion of the class defi nition living in each fi le. Compiling the source code fi les generates the entire class. This is especially useful when working with generated code, such as that generated by Visual Studio. You can augment a class without going back and changing the original code. Visual Studio .NET 2008 prefers the code-beside/partial class code representation.

The following short listings, Listing 2-1 and Listing 2-2, show two fi les that implement a singular class named SplitMe.

LISTING 2-1 Partial1.cs

// Partial1.cs

using System;

public partial class SplitMe

{

public void Method1()

{

Console.WriteLine(“SplitMe Method1”);

}

}

LISTING 2-2 Partial2.cs

// Partial2.CS

using System;

public partial class SplitMe

{

public static void Main()

{

SplitMe splitMe = new SplitMe();

splitMe.Method1();

splitMe.Method2();

}

public void Method2()

{

Console.WriteLine(“SplitMe Method2”);

}

}

To compile the previous example, you may build the project with Visual Studio, or you may use the following command line in the Visual Studio Command Prompt (if these are just loose fi les):

csc /t:exe Partial1.cs Partial2.cs This will generate an executable fi le named Partial2.exe.

After working with ASP.NET source code in the raw, it’s time to look at how Visual Studio and ASP.NET work together. Visual Studio .NET 2008 brings many new features for creating and developing Web applications, as we’ll see when working through subsequent examples.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: