Thursday, April 4, 2013

.Net Framework Architecture

The .Net Framework is an inbuilt windows component that supports Building and running the next generation of application xml web services. The .Net Framework has been developed to cater to the following objectives and Requirements:
  • To provide a consistent object-oriented environment to develop applications;
  • To provide a code execution environment that simplifies deployment and versioning;
  • To provide a code execution that eliminates the issues faced by scripted environments with respect to performance.
  • To provide a common programming model where the choice of a programming
  • Language becomes a matter of choice.
The .Net framework is made up of two components- Common Language Runtime (CLR) and the .Net Framework Class Library (FCL).The CLR is the basic for the .Net Framework and provides various services that application can use. It also forms the environment that other application run on.

The runtime is supposed to be an agent that manages code at execution time, providing core services, such as memory management, thread management, and remoteing. It also encourages security and robustness by implementing safety and other type of code accuracy. Since code management is a basic principal of the runtime is known as unmanaged code. The .Net framework consists of web Forms, Windows Forms and console applications the pertain to the presentation layer of an application.
  • Web forms are used in web-based application whereas windows forms are used in windows based application.
  • You can create a character-based console application that can be executed from the command line.
  • .Net Framework consists of two other component the .Net Framework Base classes the CLR.

Common Language Runtime
  • The Common Language Runtime (CLR) is a core component of Microsoft's .NET initiative. It is Microsoft's implementation of the Common Language Infrastructure (CLI) standard, which defines an execution environment for program code. In the CLR, code is expressed in a form of bytecode called the Common Intermediate Language (CIL, previously known as MSIL—Microsoft Intermediate Language).
  • Developers using the CLR write code in a language such as C# or VB.NET. At compile time, a .NET compiler converts such code into CIL code. At runtime, the CLR's just-in-time compiler converts the CIL code into code native to the operating system. Alternatively, the CIL code can be compiled to native code in a separate step prior to runtime by using the Native Image Generator (NGEN). This speeds up all later runs of the software as the CIL-to-native compilation is no longer necessary.
  • Although some other implementations of the Common Language Infrastructure run on non-Windows operating systems, Microsoft's implementation runs only on Microsoft Windows operating systems.
Intermediate Language

In the .NET environment, a source code which is written in any one of the programming languages should be translated into a byte code format before its execution. This byte code format is called Common Intermediate Language(CIL) or Microsoft Intermediate Language (MSIL).
Common Intermediate Language:
  • Common Intermediate Language (CIL, pronounced either "sil" or "kil") (formerly called Microsoft Intermediate Language or MSIL) is the lowest-level human-readable programming language defined by the Common Language Infrastructure specification and used by the .NET Framework and Mono. Languages which target a CLI-compatible runtime environment compile to CIL, which is assembled into bytecode. CIL is an object-oriented assembly language, and is entirely stack-based. It is executed by a virtual machine.
  • CIL was originally known as Microsoft Intermediate Language (MSIL) during the beta releases of the .NET languages. Due to standardization of C# and the Common Language Infrastructure, the bytecode is now officially known as CIL. Because of this legacy, CIL is still frequently referred to as MSIL, especially by long-standing users of the .NET languages.
JIT Compilation
  • The high level programming languages that need to be compiled require a runtime, so that the architecture on which the language runs is provided with details on how to execute its code.
  • All the programming languages use its corresponding runtime to run the application.
  • For example, To run an application developed using Visual Basic, the computer on which the application will be run must be installed with the Visual Basic runtime. The Visual Basic runtime can run only the applications developed with Visual Basic and not the ones developed with any other programming language like Java.
  • In the .NET Framework, all the Microsoft .NET languages use a common language runtime, which solves the problem of installing separate runtime for each of the programming languages. Microsoft .NET Common Language Runtime installed on a computer can run any language that is Microsoft .NET compatible.
  • The main advantage of the .NET Framework is the interoperability between different languages. As all the Microsoft .NET languages share the same common runtime language, they all work well together. For example, you can use an object written in C# from Visual Basic.NET. The same applies for all the other Microsoft .NET languages.
  • When you compile a Microsoft.NET language, the complier generates code written in the Microsoft Intermediate Language (MSIL). MSIL is a set of instructions that can quickly be translated into native code.
  • A Microsoft.NET application can be run only after the MSIL code is translated into native machine code. In .NET Framework, the intermediate language is complied "just in time" (JIT) into native code when the application or component is run instead of compiling the application at development time. The Microsoft.NET runtime consists of two JIT compilers. They are standard JIT compiler and the EconoJIT compiler. The EconoJIT compiler compiles faster than the standard JIT compiler, but the code it produces is not as optimized as the code obtained from the standard JIT compiler.
Managed Code
The code whose execution is managed by the .Net Framework Common Language Runtime is known as Managed code. It refers to a contract of cooperation between natively executing code and the runtime.The process of compiling and executing managed code in given below.
  • Compile a programs developed in a language that targets the CLR, instead of compiling the source code into machine level code, the compiler translates it into Microsoft Intermediate language(MSIL) or Intermediate language (IL).No matter which lang has been used to develop the application , it always get translated into IL. This ensure languages interoperability.
  • In addition to translating the code into IL, the compiler also produce metadata about the program during the process of compilation. Meta data contains the description of the program
  • The IL and the metadata are linked in an assembly.
  • The compiler creates the .EXE or .DLL file.
  • Execute the .Exe or .DLL file, the code and all the other relevant information from the base class library is sent to the class loader. The class loader the code in the memory.
  • Before the code can be executed, the .Net framework needs to convert the IL into native or CPU-specific code. The Just-In-Time (JIT) compiler translator the code from IL into managed native code.
  • The CLR supplies a JIT compiler for each supported CPU architecture.
  • During JIT compilation, the code is also checked for type safety. Type safety ensures that objects are always accessed in a compatible way, you try to pass an 8-byte value to a method the accepts a 4-byte value as it parameter.
  • After translating the IL into native code, the converted code is sent to the .Net runtime manager.
  • The .Net runtime manager executes the code. While executing the code, security check the performed to ensure that the code has the appropriate permissions for accessing the available resources.

Metadata
  • Metadata is binary information describing your program that is stored either in a common language runtime portable executable (PE) file or in memory. When you compile your code into a PE file, metadata is inserted into one portion of the file, while your code is converted to Microsoft intermediate language (MSIL) and inserted into another portion of the file.
  • Every type and member defined and referenced in a module or assembly is described within metadata. When code is executed, the runtime loads metadata into memory and references it to discover information about your code's classes, members, inheritance, and so on.
Benefits of metadata:

     Metadata is the key to a simpler programming model, eliminating the need for Interface Definition Language (IDL) files, header files, or any external method of component reference. Metadata allows .NET languages to describe themselves automatically in a language-neutral manner, unseen by both the developer and the user. Additionally, metadata is extensible through the use of attributes. Metadata provides the following major benefits:



Self-describing files:

  • Common language runtime modules and assemblies are self-describing. A module's metadata contains everything needed to interact with another module.
  • Metadata automatically provides the functionality of IDL in COM, allowing you to use one file for both definition and implementation. Runtime modules and assemblies do not even require registration with the operating system.
  • As a result, the descriptions used by the runtime always reflect the actual code in your compiled file, which increases application reliability.
Language Interoperability and easier component-based design:

  • Metadata provides all the information required about compiled code for you to inherit a class from a PE file written in a different language.
  • You can create an instance of any class written in any managed language (any language that targets the common language runtime) without worrying about explicit marshaling or using custom interoperability code.
Attributes:

  • The .NET Framework allows you to declare specific kinds of metadata, called attributes, in your compiled file. Attributes can be found throughout the .NET Framework and are used to control in more detail how your program behaves at run time.
  • Additionally, you can emit your own custom metadata into .NET Framework files through user-defined custom attributes. For more information, see Extending Metadata Using Attributes.
Metadata Tools:
Several tools are used to handle metadata.
  • Reflection is a tool which is used to handle metadata.
  • Third party tools (Ex : Post sharp, Mono ceil) are used to retrieve and manipulate metadata.
  • Manifest is a text file that contains metadata about .NET assemblies.
Automatic Memory Management

Automatic memory management is one of the services that the common language runtime provides during Managed Execution. The common language runtime's garbage collector manages the allocation and release of memory for an application. For developers, this means that you do not have to write code to perform memory management tasks when you develop managed applications. Automatic memory management can eliminate common problems, such as forgetting to free an object and causing a memory leak, or attempting to access memory for an object that has already been freed. This section describes how the garbage collector allocates and releases memory.
  1. Allocating Memory
  2. Releasing Memory
  3. Generation and Performance
  4. Releasing memory for Unmanaged Resource

6 comments: