<<

UNIT I - .NET AND FRAME WORK

The - The Common Language Specification- The - Intermediate Language- Metadata- Code-Managed - Side−by−Side Execution- Understanding Assemblies- Assembly- - Strong Names Introduction to Visual Basic .NET Development environment.

COMMON TYPE SYSTEM

A key piece of functionality that enables Multiple Language Support is a Common Type System, in which all commonly used data types, even base types such as Longs and Booleans are actually implemented as objects. Since all languages are using the same of types, calling one language from another doesn't require type conversion or weird calling conventions.

For example, what we called an Integer in VB6 and earlier, is now known as a Short in Visual Basic.NET.

The common type system defines how types are declared, used, and managed in the runtime, and is also an important part of the runtime's support for cross-language integration. The common type system performs the following functions:

 Establishes a framework that helps enable cross-language integration, type safety, and high performance code execution.  Provides an object-oriented model that supports the complete implementation of many programming languages.  Defines rules that languages must follow, which helps ensure that objects written in different languages can interact with each other.

THE COMMON LANGUAGE SPECIFICATION

Languages that fit into.NET must satisfy the Common Language Specification (CLS), which sets the constraints the languages must meet. If a language adheres to this spec, it gets an appropriate level of language interoperability.

There are actually three categories of compliance to the CLS that .NET languages can subscribe to. Briefly they are:  Compliant producer – The components in the language can be used by any other language.  Consumer – The language can reuse classes produced in any other language. This basically means the ability to instantiate classes in the way that, for example, scripting languages can instantiate COM objects today.  Extender – Languages in this category can do more than just instantiate classes – they can also extend those classes using the inheritance features of .NET.

All the confirmed languages in the Visual Studio suite (VB, VC++, #) are expected to satisfy all three categories of the CLS. Third party languages can select the levels of compliance that make sense for them.

The .NET Framework includes classes, interfaces, and value types that expedite and optimize the development process and provide access to system functionality. To facilitate interoperability between languages, the .NET Framework types are CLS-compliant and can therefore be used from any whose conforms to the common language specification (CLS).

The .NET Framework types are the foundation on which .NET applications, components, and controls are built. The .NET Framework includes types that perform the following functions:

 Represent base data types and exceptions.  Encapsulate data structures.  Perform I/O.  Access information about loaded types.  Invoke .NET Framework security checks.  Provide data access, rich client-side GUI, and server-controlled, client-side GUI.

The .NET Framework provides a rich set of interfaces, as well as abstract and concrete (non-abstract) classes. You can use the concrete classes as is or, in many cases, derive your own classes from them. To use the functionality of an interface, you can either create a class that implements the interface or derive a class from one of the .NET Framework classes that implements the interface. THE COMMON LANGUAGE RUNTIME

The Common Language Runtime is therefore the environment in which we run our .NET applications that have been compiled to a common language, namely Microsoft Intermediate Language (MSIL), often referred to simply as IL.

At the base is the Common Language Runtime, often abbreviated to CLR. This is the heart of the .NET framework, the engine that drives key functionality. It includes, for example, a common system of data types. These common types, plus a standard interface convention, make cross-language inheritance possible. In addition to allocation and management of memory, the CLR also does reference counting for objects, and handles garbage collection.

Common Type System Intermediate Language Execution Support Security Garbage Collection, Code Manager Class , Memory Layout

That small part in the middle, called Execution Support, contains most of the capabilities normally associated with a language runtime (such as the VBRUNxxx.DLL runtime used with Visual Basic). The rest is new, at least for Microsoft platforms.

The design of the CLR is based on the following goals:

 Simpler, faster development  Automatic handling of memory management and process communication  Good tool support  Scalability

The most ambitious aspect of the CLR is that it is designed to support multiple languages and allow unprecedented levels of integration among those languages. By enforcing a common type system, and by having complete control over interface calls, the CLR allows languages to work together more transparently than ever before. Previously, one language could instantiate and use components written in another language by using COM. Sometimes calling conventions were difficult to manage, especially when Visual Basic was involved, but it could generally be made to work.

It is straightforward in the .NET Framework to use one language to subclass a class implemented in another language. A class written in Visual Basic can inherit from a base class written in C++, or in COBOL for that matter (at least one major vendor is at work on a COBOL implementation for .NET). The VB program doesn't even need to know the language used for the base class, and we're talking full implementation inheritance with no problems requiring recompilation when the base class changes.

How can this work? The information furnished by makes it possible. There is no Interface Definition Language (IDL) in .NET because none is needed. A class interface looks the same, regardless of the language that generated it. The CLR uses metadata to manage all the interfaces and calling conventions between languages.

MICROSOFT INTERMEDIATE LANGUAGE

Microsoft Intermediate Language (MSIL) represents the transient stage in the process of conversion of written in any .NET language to machine language as a pseudo- language code that's between the source code you write-such as Visual Basic .NET or C#-and Intel-based or . When you compile a .NET program, , the compiler translates your source code into Microsoft intermediate language (MSIL), which is a CPU-independent set of instructions that can be efficiently converted to native code. When we execute the code, MSIL is converted to CPU-specific code, usually by a just-in-time (JIT) compiler. Because the common language runtime supplies one or more JIT , the same set of MSIL can be JIT-compiled and executed on any supported architecture.

During compilation of .NET programming languages, the source code is translated into MSIL code rather than machine-specific object code. MSIL is a CPU- and platform-independent instruction set that can be executed in any environment supporting the .NET framework. MSIL code is verified for safety during runtime, providing better security and reliability than natively compiled binaries

METADATA Metadata is defined as "data about data". The metadata is generated by a compiler and stored automatically in an EXE or DLL. It's in binary, but the framework offers an API to export metadata to and from an XML schema or a COM type library.

Some of the items in the metadata defined for the .NET framework are:

• Description of a deployment unit (called an assembly)

 Name, version, culture (which could determine, for example, the default user language)  A public key for verification  Types exported by the assembly  Dependencies – other assemblies which this assembly depends upon  Security permissions needed to run

• Base classes and interfaces used by the assembly

• Custom attributes

 User defined (inserted by the developer)  Compiler defined (inserted by the compiler to indicate something special about the language)

Compilers are some of the most extensive users of metadata. For example, a compiler can examine a module produced by a different compiler and use the metadata for cross-language type import. It can also produce metadata about its own compiled modules, including such elements as flags that a module has compiled for debugging, or a language-specific marker.

Even information that might appear in a tool tip can be embedded in metadata. This extendable data store about a compiled module greatly facilitates the simpler deployment available under the .NET Framework. An API, called the Reflection API, is available for scanning and manipulation of metadata elements

Managed execution process includes the following steps:

Choosing the right compiler

.Net Framework is a Multilanguage execution environment, the runtime supports a wide variety of data types and language features. In order to obtain the full benefits provided by the common language runtime , you should use one or more language (VB.Net, C# etc.) compilers that target the runtime

Compiling the code to MSIL

Unlike the execution style of compiling source code into machine level code, .Net language compilers translate the source code into Microsoft Intermediate Language. This ensures language interoperability because no matter which language has been used to develop the application, it always gets translated to Microsoft Intermediate Language. During the the compiler produces metadata, that contains description of the program like dependencies, versions etc

Compiling MSIL to native code

Before the program execution, Just In Time compiler (JIT) compiles the MSIL into native code and stores it in a memory buffer. During JIT compilation, the code is also checked for type safety. Type safety ensures that objects are always accessed in a compatible way. The compiled native code is in memory and is not persisted. So every time we run our application this whole thing has to happen again.

EXECUTION OF CODE

After translating the IL into native code, it is sent to .Net runtime manager. The .Net runtime manager executes the code. During execution, managed code receives services such as garbage collection, security, interoperability with unmanaged code, cross-language debugging support, and enhanced deployment and versioning support.

SIDE-BY-SIDE EXECUTION IN THE .NET FRAMEWORK

Side-by-side execution is the ability to run multiple versions of an application or component on the same computer. You can have multiple versions of the common language runtime, and multiple versions of applications and components that use a version of the runtime, on the same computer at the same time.

The following illustration shows several applications using two different versions of the runtime on the same computer. Applications A, B, and C use runtime version 1.0, while application D uses runtime version 1.1.

Side-by-side execution of two versions of the runtime

The .NET Framework consists of the common language runtime and a collection of assemblies that contain the API types. The runtime and the .NET Framework assemblies are versioned separately. For example, version 4.0 of the runtime is actually version 4.0.319, while version 1.0 of the .NET Framework assemblies is version 1.0.3300.0.

The following illustration shows several applications using two different versions of a component on the same computer. Application A and B use version 1.0 of the component while Application C uses version 2.0 of the same component.

Side-by-side execution of two versions of a component

Side-by-side execution gives you more control over which versions of a component an application binds to, and more control over which version of the runtime an application uses.

Benefits of Side-by-Side Execution

Prior to Windows XP and the .NET Framework, DLL conflicts occurred because applications were unable to distinguish between incompatible versions of the same code. Type information contained in a DLL was bound only to a file name. An application had no way of knowing if the types contained in a DLL were the same types that the application was built with. As a result, a new version of a component could overwrite an older version and break applications.

Side-by-side execution and the .NET Framework provide the following features to eliminate DLL conflicts:

Strong-named assemblies.

Side-by-side execution uses strong-named assemblies to bind type information to a specific version of an assembly. This prevents an application or component from binding to an invalid version of an assembly. Strong-named assemblies also allow multiple versions of a file to exist on the same computer and to be used by applications.

Version-aware code storage. The .NET Framework provides version-aware code storage in the . The global assembly cache is a computer-wide code cache present on all computers with the .NET Framework installed. It stores assemblies based on version, culture, and publisher information, and supports multiple versions of components and applications.

Isolation.

Using the .NET Framework, you can create applications and components that execute in isolation. Isolation is an essential component of side-by-side execution. It involves being aware of the resources you are using and sharing resources with confidence among multiple versions of an application or component. Isolation also includes storing files in a version-specific way.

ASSEMBLIES

Assembly is the smallest unit of deployment of a .net application. It can be a dll or an exe. There are mainly two types to it:

Private Assembly: The dll or exe which is sole property of one application only. It is generally stored in application root folder

Public/Shared assembly: It is a dll which can be used by multiple applications at a time. A shared assembly is stored in GAC i.e Global Assembly Cache.

ELEMENTS OF THE .NET FRAMEWORK:

The .NET Framework is the infrastructure for the Microsoft .NET platform.

The .NET Framework is an environment for building, deploying, and running Web applications and Web Services.

The .NET Framework contains a common language runtime and common class libraries - like ADO.NET, ASP.NET and - to provide advanced standard services that can be integrated into a variety of computer systems.

The .NET Framework provides a feature-rich application environment, simplified development and easy integration between a numbers of different development languages. The .NET Framework is language neutral. Currently it supports C++, C#, Visual Basic, and JScript (Microsoft's version of JavaScript).

Microsoft's Visual Studio.NET is a common development environment for the .NET Framework.

The major components of the .NET framework are shown in the following diagram:

The framework starts all the way down at the memory management and component loading level, and goes all the way up to multiple ways of rendering user and program interfaces. In between, there are layers that provide just about any system-level capability that a developer would need.

Components of the .NET Framework

A. Common Language Runtime

B. .NET Class Library

C. Unifying components

Just-In-Time (JIT) Compilation This technique involves the byte-code being turned into code immediately executable by the CPU. The conversion is performed gradually during the program's execution. JIT compilation provides environment-specific optimization, runtime type safety, and assembly verification. To accomplish this, the JIT compiler examines the assembly metadata for any illegal accesses and handles violations appropriately.

NGEN () Compilation

NGEN allows producing a native binary image for the current environment. The byte-code is either skipped entirely or converted into native CPU instructions completely before runtime. This eliminates the JIT overhead at the expense of portability; whenever an NGEN-generated image is run in an incompatible environment, .NET framework automatically reverts to using JIT.

Once NGEN is run against an assembly, the resulting native image is placed into the Global Assembly Cache for use by all other .NET assemblies. It is advised that NGEN is run during applications' deployment.

.NET CLASS LIBRARY:

The .NET Class Library was described as containing hundreds of classes that model the system and services it provides. To make the .NET Class Library easier to work with and understand, it's divided into namespaces. The root namespace of the .NET Class Library is called System, and it contains core classes and data types, such as Int32, Object,Array, and Console. Secondary namespaces reside within the System namespace.

The middle layer includes the next generation of standard system services such as ADO.NET and XML. These services are brought under the control of the framework, making them universally available and standardizing their usage across languages.

Examples of nested namespaces include the following:

 System.Diagnostics: Contains classes for working with the Event Log

 System.Data: Makes it easy to work with data from multiple data sources (System.Data.OleDb resides within this namespace and contains the ADO.NET classes)

 System.IO: Contains classes for working with files and data streams Figure 1-2 illustrates the relationship between some of the major namespaces in the.NET Class Library

System.Data

Data Set Data View System.Diagnostics System.io

Debug Eventlog Directory File Stream

The benefits of using the .NET Class Library include a consistent set of services available to all .NET languages and simplified deployment, because the .NET ClassLibrary is available on all implementations of the .NET Framework.

Features of .NET The .NET a wonderful platform for developing modern applications.

Rich Functionality out of the box

.NET framework provides a rich set of functionality out of the box. It contains hundreds of classes that provide variety of functionality ready to use in your applications. This means that as a developer you need not go into low level details of many operations such as file IO, network communication and so on.

Easy development of web applications

ASP.NET is a technology available on .NET platform for developing dynamic and data driven web applications. ASP.NET provides an event driven programming model (similar to Visual Basic 6 that simplify development of web pages (now called as web forms) with complex user interface. ASP.NET server controls provide advanced user interface elements (like calendar and grids) that save lot of coding from programmer’s side.

OOPs Support The advantages of Object Oriented programming are well known. .NET provides a fully object oriented environment. The philosophy of .NET is – “Object is mother of all.”

Languages like Visual Basic.NET now support many of the OO features that were lacking traditionally. Even primitive types like integer and characters can be treated as objects – something not available even in OO languages like C++.

Multi-Language Support

Generally enterprises have varying skill sets. For example, a company might have people with skills in Visual Basic, C++, and Java etc. It is an experience that whenever a new language or environment is invented existing skills are outdated. This naturally increases cost of training and learning curve.

The .NET provides something attractive in this area. It supports multiple languages. This means that if you have skills in C++, you need not throw them but just mould them to suit .NET environment.

Currently four languages are available right out of the box namely – Visual Basic.NET, C# (pronounced as C-sharp), Jscript.NET and Managed C++ (a dialect of Visual C++). There are many vendors that are working on developing language compilers for other languages (20+ language compilers are already available).

The beauty of multi language support lies in the fact that even though the syntax of each language is different, the basic capabilities of each language remain at par with one another.

Multi-Device Support

Modern life style is increasingly embracing mobile and wireless devices such as PDAs, mobiles and handheld PCs. .NET provides promising platform for programming such devices. .NET Compact Framework and Mobile Internet Toolkit are step ahead in this direction.

Automatic memory management

While developing applications developers had to develop an eye on system resources like memory. Memory leaks were major reason in failure of applications. .NET takes this worry away from developer by handling memory on its own. The garbage collector takes care of freeing unused objects at appropriate intervals. Compatibility with COM and COM+

Before the introduction of .NET, COM was the de-facto standard for componentized software development. Companies have invested lot of money and efforts in developing COM components and controls.

The good news is you can still use COM components and ActiveX controls under .NET. This allows you to use your existing investment in .NET applications. .NET still relies on COM+ for features like transaction management and object pooling. In fact it provides enhanced declarative support for configuring COM+ application right from your source code. Your COM+ knowledge still remains as a valuable asset.

No more DLL Hell

If you have worked with COM components, you probably are aware of “DLL hell”. DLL conflicts are a common fact in COM world. The main reason behind this was the philosophy of COM “one version of component across machine”. Also, COM components require registration in the system registry. The .NET ends this DLL hell by allowing applications to use their own copy of dependent DLLs. Also, .NET components do not require any kind of registration in system registry.

Strong XML support Now days it is hard to find a programmer who is unaware of XML. XML has gained such a strong industry support that almost all the vendors have released some kind of upgrades or patches to their existing software to make it “XML compatible”.

Currently, .NET is the only platform that has built with XML right into the core framework. .NET tries to harness power of XML in every possible way.

In addition to providing support for manipulating and transforming XML documents, .NET provides XML web services that are based on standards like HTTP, XML and SOAP.

Ease of deployment and configuration Deploying windows applications especially that used COM components were always been a tedious task. Since .NET does not require any registration as such, much of the deployment is simplified. This makes XCOPY deployment viable.

Configuration is another area where .NET – especially ASP.NET – shines over traditional languages. The configuration is done via special files having special XML vocabulary. Since, most of the configuration is done via configuration files, there is no need to sit in front of actual machine and configure the application manually.

Security

Windows platform was always criticized for poor security mechanisms. Microsoft has taken great efforts to make .NET platform safe and secure for enterprise applications. Features such as type safety, and role based authentication make overall application more robust and secure.