Monday, 23 December 2013

ASP.NET Fundamentals: ASP.NET is Compiled, Not Interpreted Language

ASP.NET Fundamentals: ASP.NET is Compiled, Not Interpreted Language

ASP.NET applications, like all .NET applications, are always compiled. In fact, it’s impossible to execute C# or Visual Basic code without it being compiled first. 

.NET applications actually go through two stages of compilation. In the first stage, the C# code you write is compiled into an intermediate language called Microsoft Intermediate Language (MSIL), or just IL. This first step is the fundamental reason that .NET can be language-interdependent. Essentially, all .NET languages (including C#, Visual Basic, and many more) are compiled into virtually identical IL code. This first compilation step may happen automatically when the page is first requested, or you can perform it in advance (a process known as pre-compiling). The compiled file with IL code is an assembly. 

The second level of compilation happens just before the page is actually executed. At this point, the IL code is compiled into low-level native machine code. This stage is known as just-in-time (JIT) compilation, and it takes place in the same way for all .NET applications (including Windows applications, for example).

.NET compilation is decoupled into two steps in order to offer developers the most convenience and the best portability. Before a compiler can create low-level machine code, it needs to know what type of operating system and hardware platform the application will run on (for example, 32-bit or 64-bit Windows). By having two compile stages, you can create a compiled assembly with .NET code and still distribute this to more than one platform.

Of course, JIT compilation probably wouldn’t be that useful if it needed to be performed every time a user requested a web page from your site. Fortunately, ASP.NET applications don’t need to be compiled every time a web page is requested. Instead, the IL code is created once and regenerated only when the source is modified. Similarly, the native machine code files are cached in a system directory that has a path like c:\Windows\Microsoft.NET\Framework\[Version]\Temporary ASP.NET Files.

The actual point where your code is compiled to IL depends on how you’re creating and deploying your web application. If you’re building a web project in Visual Studio, the code is compiled to IL when you compile your project. But if you’re building a lighter-weight project less website, the code for each page is compiled the first time you request that page. Either way, the code goes through its second compilation step (from IL to machine code) the first time it’s executed.

ASP.NET also includes pre-compilation tools that you can use to compile your application right down to machine code once you’ve deployed it to the production web server. This allows you to avoid the overhead of first-time compilation when you deploy a finished application (and prevent other people from tampering with your code).

No comments:

Post a Comment