Wednesday, August 4, 2010

ASP.NET: Framework & CLR FAQ’s

What is the .NET Framework?

The .NET Framework is set of technologies that form an integral part of the .NET Platform. It is Microsoft's managed code programming model for building applications that have visually stunning user experiences, seamless and secure communication, and the ability to model a range of business processes.
 

The .NET Framework has two main components: the common language runtime (CLR) and .NET Framework class library. The CLR is the foundation of the .NET framework and provides a common set of services for projects that act as building blocks to build up applications across all tiers. It simplifies development and provides a robust and simplified environment which provides common services to build application. The .NET framework class library is a collection of reusable types and exposes features of the runtime. It contains of a set of classes that is used to access common functionality.

What is CLR?

The .NET Framework provides a runtime environment called the Common Language Runtime or CLR. The CLR can be compared to the Java Virtual Machine or JVM in Java. CLR handles the execution of code and provides useful services for the implementation of the program. In addition to executing code, CLR provides services such as memory management, thread management, security management, code verification, compilation, and other system services. It enforces rules that in turn provide a robust and secure execution environment for .NET applications.


Full form of CLR is Common Language Runtime and it forms the heart of the .NET framework.All Languages have runtime and its the responsibility of the runtime to take care of the code execution of the program.For example VC++ has MSCRT40.DLL,VB6 has MSVBVM60.DLL , Java has Java Virtual Machine etc. Similarly .NET has CLR.Following are the responsibilities of CLR:
√ Garbage Collection :- CLR automatically manages memory thus eliminating memory leakes. When objects are not referred GC automatically releases those memory thus providing efficient memory  management.
√ Code Access Security :- CAS grants rights to program depending on the security configuration of the machine.Example the program has rights to edit or create a new file but the security configuration of machine does not allow the program to delete a file.CAS will take care that the code runs under the environment of machines security configuration.
√ Code Verification :- This ensures proper code execution and type safety while the code runs.It prevents the source code to perform illegal operation such as accessing invalid memory locations etc.  

√ IL( Intermediate language )-to-native translators and optimizer's :- CLR uses JIT and compiles the IL code to machine code and then executes. CLR also determines depending on platform what is optimized way of running the IL code.

What is CTS?

Common Type System (CTS) describes the datatypes that can be used by managed code. CTS defines how these types are declared, used and managed in the runtime. It facilitates crosslanguage integration, type safety, and high performance code execution. The rules defined in CTS can be used to define your own classes and values.

In order that two language communicate smoothly CLR has CTS (Common Type System).Example in VB you have "Integer" and in C++ you have "long" these datatypes are not compatible so the interfacing between them is very complicated. In order that two different languages can communicate Microsoft introduced Common Type System. So "Integer" datatype in VB6 and "int" datatype in C++ will convert it to System.int32 which is datatype of CTS.CLS which is covered in the coming question is subset of CTS.


If you have undergone COM programming period interfacing VB6 application with VC++
application was a real pain as the datatype of both languages did not have a common ground
where they can come and interface , by having CTS interfacing is smooth.

What is CLS?

Common Language Specification (CLS) defines the rules and standards to which languages must adhere to in order to be compatible with other .NET languages. This enables C# developers to inherit from classes defined in VB.NET or other .NET compatible languages.

What is managed code?

The .NET Framework provides a run-time environment called the Common Language Runtime, which manages the execution of code and provides services that make the development process easier. Compilers and tools expose the runtime's functionality and enable you to write code that benefits from this managed execution environment. The code that runs within the common language runtime is called managed code.

What is MSIL?

When the code is compiled, the compiler translates your code into Microsoft intermediate language (MSIL). The common language runtime includes a JIT compiler for converting this MSIL then to native code.
MSIL contains metadata that is the key to cross language interoperability. Since this metadata is standardized across all .NET languages, a program written in one language can understand the metadata and execute code, written in a different language. MSIL includes instructions for loading, storing, initializing, and calling methods on objects, as well as instructions for arithmetic and logical operations, control flow, direct memory access, exception handling, and other operations.

What is JIT?

JIT is a compiler that converts MSIL to native code. The native code consists of hardware specific instructions that can be executed by the CPU. Rather than converting the entire MSIL (in a portable executable[PE]file) to native code, the JIT converts the MSIL as it is needed during execution. This converted native code is stored so that it is accessible for subsequent calls.

What is portable executable (PE)?

PE is the file format defining the structure that all executable files (EXE) and Dynamic Link Libraries (DLL) must use to allow them to be loaded and executed by Windows. PE is derived from the Microsoft Common Object File Format (COFF). The EXE and DLL files created using the .NET Framework obey the PE/COFF formats and also add additional header and data sections to the files that are only used by the CLR.

What is an assembly?

An assembly is a collection of one or more .exe or dll's. An assembly is the fundamental unit for application development and deployment in the .NET Framework. An assembly contains a collection of types and resources that are built to work together and form a logical unit of functionality. An assembly provides the CLR with the information it needs to be aware of type implementations.

Assembly - An assembly may be an exe, a dll, an application having an entry point, or a library. It may consist of one or more files. It represents a group of resources, type definitions, and implementation of these types. They may contain references to other assemblies. These resources, types & references are compacted in a block of data called manifest. The manifest is a part of the assembly, which makes it self-describing. Assemblies also increase security of code in .NET. An assembly maybe shared(public) or private. The assembly, overall comprises of 3 entities: IL, Manifest, Metadata. Metadata describes IL, whereas Manifest describes the assembly. An assembly may be created by building the class(the .vb or .cs file), thereby producing its DLL.  

ILDASM - The contents of an assembly may be viewed using the ILDASM tool, that comes with the .NET SDK or the Visual Studio.NET. The ildasm.exe tool may also be used in the command line compiler.

What are the contents of assembly?

A static assembly can consist of four elements:
・ Assembly manifest - Contains the assembly metadata. An assembly manifest contains the information about the identity and version of the assembly. It also contains the information required to resolve references to types and resources.
・ Type metadata - Binary information that describes a program.
・ Microsoft intermediate language (MSIL) code.
・ A set of resources.

What are the different types of assembly?

Private Assembly: A private assembly is installed in the installation directory of an application and is accessible to that application only.
Shared Assembly: A shared assembly is shared by multiple applications. A shared assembly has a strong name and is installed in the GAC.
We also have satellite assemblies that are often used to deploy language-specific resources for an application.

What is a dynamic assembly?

A dynamic assembly is created dynamically at run time when an application requires the types within these assemblies.

What is a strong name?

You need to assign a strong name to an assembly to place it in the GAC and make it globally accessible. A strong name consists of a name that consists of an assembly's identity (text name, version number, and culture information), a public key and a digital signature generated over the assembly. The .NET Framework provides a tool called the Strong Name Tool (Sn.exe), which allows verification and key pair and signature generation.

What is GAC? What are the steps to create an assembly and add it to the GAC?

The global assembly cache (GAC) is a machine-wide code cache that stores assemblies specifically designated to be shared by several applications on the computer. You should share assemblies by installing them into the global assembly cache only when you need to.  

Steps
- Create a strong name using sn.exe tool eg: sn -k mykey.snk
- in AssemblyInfo.cs, add the strong name eg: [assembly: AssemblyKeyFile("mykey.snk")]
- recompile project, and then install it to GAC in two ways :
・ drag & drop it to assembly folder (C:\WINDOWS\assembly OR C:\WINNT\assembly)
(shfusion.dll tool)
・ gacutil -i abc.dll : Using Gacutil. Goto "Visual Studio Command Prompt" and type "gacutil –i
(assembly_name)".Where (assembly_name) is the DLL name of the project.

Interoperability.

Enterprises that have written their business solutions using the old native COM technology need a way for re-using these components in the new .NET environment. .NET components communicate with COM using RCW (Runtime Callable Wrapper (RCW).
To use a COM component,
* Right click the Project & click on Add References.
* Select the COM tab
* Select the COM component
Another way of using a COM component is using the tblimp.exe tool (Type Library Import). Using the COM component directly in the code may be achieved by using System.Runtime.InteropServices namespace. This contains class TypeLib Converter which provides methods to convert COM classes and interface in to assembly metadata. If the COM component does not have a Type Library, then custome wrappers need to be created. Once the COM wrapper is created, it has to be registered in the registry.

How to call .NET component from COM?

In case a .NET component needs to be used in COM, we make use of COM Callable Wrapper
(CCW). Following are the different approaches to implement it:
* Explicitly declare interfaces.
* The second way to create CCW using InteropServices attributes.Here interfaces are created
automatically.

What is an IL?

Twist :- What is MSIL or CIL , What is JIT?
(IL)Intermediate Language is also known as MSIL (Microsoft Intermediate Language) or CIL (Common Intermediate Language). All .NET source code is compiled to IL. This IL is then converted to machine code at the point where the software is installed, or at run-time by a Just-In- Time (JIT) compiler.

What is a Managed Code?

Managed code runs inside the environment of CLR i.e. .NET runtime.In short all IL are managed code.But if you are using some third party software example VB6 or VC++ component they are unmanaged code as .NET runtime (CLR) does not have control over the source code execution of the language.

What is an Assembly?

√ Assembly is unit of deployment like EXE or a DLL.
√ An assembly consists of one or more files (dlls, exe's, html files etc.), and
represents a group of resources, type definitions, and implementations of those types. An assembly may also contain references to other assemblies. These resources, types and references are described in a block of data called a manifest. The manifest is part of the
assembly, thus making the assembly self-describing.
√ An assembly is completely self-describing.An assembly contains metadata information, which is used by the CLR for everything from type checking and security to actually invoking the components methods.As all information is in assembly itself it is independent of registry.This is the basic advantage as compared to COM
where the version was stored in registry.
√ Multiple versions can be deployed side by side in different folders. These different versions can execute at the same time without interfering with each other.Assemblies can be private or shared. For private assembly deployment,the assembly is copied to the same directory as the client program that references it.No registration is needed, and no fancy installation program is required. When the component is removed, no registry cleanup is needed,and no uninstall program is required. Just delete it from the hard drive.
√ In shared assembly deployment, an assembly is installed in the Global Assembly Cache (or GAC). The GAC contains shared assemblies that are globally accessible to all .NET applications on the machine.

What are different types of Assembly?

There are two types of assembly Private and Public assembly.A private assembly is normally used by a single application, and is stored in the application's directory, or a sub-directory eneath. A shared assembly is normally stored in the global assembly cache, which is a repository of assemblies maintained by the .NET runtime. Shared assemblies are usually libraries of code which many applications will find useful, e.g. Crystal report classes which will be used by all application for Reports..

What is NameSpace?

Namespace has two basic functionality :-
√ NameSpace Logically group types.Example System.Web.UI logically groups our UI related features.
√ In Object Oriented world may times its possible that programmers will use the same class name.By qualifying NameSpace with classname this collision can be removed

What is Manifest?

Assembly metadata is stored in Manifest.Manifest contains all the metadata needed to do the following things( See Figure Manifest View for more details):
√ Version of assembly
√ Security identity
√ Scope of the assembly
√ resolve references to resources and classes.
√ The assembly manifest can be stored in either a PE file (an .exe or .dll) with Microsoft intermediate language (MSIL) code or in a stand-alone PE file that contains only assembly manifest information

What is concept of strong names ?

Twist :- How do we generate strong names or what is the process of generating strong names, What is use of SN.EXE , How do we apply strong names to assembly ? , How do you sign an assembly ?
Strong name is similar to GUID(It is supposed to be unique in space and time) in COM components.Strong Name is only needed when we need to deploy assembly in GAC. Strong Names helps GAC to differentiate between two versions.Strong names use public key cryptography (PKC) to ensure that no one can spoof it.PKC use public key and private key concept. Following are the step to generate a strong name and sign a assembly :-
√ Go to "Visual Studio Command Prompt"
√ After you are in command prompt type sn.exe -k "c:\test.snk".
√ After generation of the file you can view the SNK file in a simple notepad.
√ After the SNK file is generated its time to sign the project with this SNK file.
√ Click on project properties and the browse the SNK file to the respective folder and compile
the project.

What are Value types and Reference types ?

Value types directly contain their data are either allocated on the stack or allocated in-line in a structure.
Reference types store a reference to the value's memory address, and are allocated on the heap. Reference types can be self-describing types, pointer types, or interface types. Variables that are value types each have their own copy of the data, and therefore operations on one variable do not affect other variables. Variables that are reference types can refer to the same object; therefore, operations on one variable can affect the same object referred to by another variable.All types derive from the System.Object base type.

What is concept of Boxing and Unboxing ?

Boxing permits any value type to be implicitly converted to type object or to any interface type implemented by value type.Boxing is process in which a object instances created and copying value types value in to that instance. Unboxing is vice versa of boxing operation where the value is copied from the instance in to
appropriate storage location. Below is sample code of boxing and unboxing where integer data type is converted in to object and then vice versa.
Dim x As Integer
Dim y As Object
x = 10
' boxing process
y = x
' unboxing process
x = y

1 comment:

rmouniak said...
This comment has been removed by the author.

Post a Comment