Tuesday 11 February 2014

Assemblies Concept in .Net

What is Assemblies in .Net?

  • Assembly is a logical collection of smallest unit in .Net Framework.
  • Example: .DLL File contains the application code, the .aspx file, .ascx user controls, .gif, .bmp, .ico file and other files like Resource file (.resx), etc.
  • In summary, Assemblies is a basic fundamental unit of application development and deployment in the .Net Framework
  • An assembly contains the MSIL code, which the common language runtime executes, and the type metadata.
  • An assembly also contains an assembly manifest that contains the assembly metadata. This metadata contains information about the assembly version, its security identity, the resources required by the assembly, and the scope of the assembly.
  • Assemblies are the smallest units to which the .NET Framework grants permissions. They provide security boundaries within the .NET Framework. You specify the permission required by your application while building assemblies. When the assembly is loaded into the runtime, the assembly sends a request to the runtime to grant the permission.
So now, lets Get Started with Assemblies .Net and understand concept of Assemblies in .Net by easy step by step explanation.

What is Assembly Manifest?

  • Assembly Manifest contains the metadata for the assembly.
  • Example, Assembly Manifest contains
  • Name and Version Information of Assembly.
  • Files (or Resource) that are made up the assembly.
  • Set of permissions required for the assembly to run properly.
  • And few other details depends on assembly.

How Assembly Manifest is useful?


When the Common Language Runtime (CLR) loads an assembly, it first reads the manifest to get the information regarding assembly.

Benefits of Assemblies?

  • It is used in Versioning
  • For Type Identification
  • For Security
  • For Deployment
  • For Referencing.
  • Improving Application Performance
  • Better Code Management and Maintenance

What is "DLL Hell" Problem?
"DLL Hell" problem occurs due to version incompatibility of dll's.

DLL HELL is the problem that occures when an installation of a newer application might break or hinder other application as newer DLLs are copied into the system and the older application do not support or not compatible with them. .net overcomes this problem by supporting multiple versions of an assembly at any given time.this is called side-by-side component versioning.

More on "DLL Hell" Problem.
"DLL Hell" is a problem occurs when multiple applications attempt to share a common component like a dynamic link library (DLL) or a Component Object Model (COM) class. In the most typical case, one application will install a new version of the shared component that is not backward compatible with the version already on the machine. Although the application that has just been installed works well, existing applications that depended on a previous version of the shared component might no longer work. 

How Assembly used in Avoiding "DLL Hell" Problem?

"DLL Hell" Problem is solved in .Net using Assembly.

Each assembly has a version number. All the types and resources in an assembly share the same version number to make it easy for applications to refer to the correct version of files and avoid problem of "DLL Hell". Thus assemblies allows the execution of multiple versions of the same assembly on the same machine. The side-by-side execution of assemblies overcomes the problem known as "DLL hell," which is one of the major problems associated with COM applications.


How Assembly can Improve Application Performance?
CLR is place where .Net Application codes gets compiled. Whenever you made an request to ASP.NET page, the page gets compiled first and then is transferred to the user. The compilation process is also completed in two steps.

  • 1. An IL (Intermediate Language) is generated and then this is handed over for JIT
  • 2. JIT (Just In Time) compilation which produces the machine code and our pages get displayed with their dynamic content.

The performance of our web application can be improved by creating pre-compiled libraries of IL which can then be handed over to JIT directly without having the inclusion of an extra process to make the conversion. This method is called componentization. Components are pre-compiled set of classes that have been developed in the form of DLL files and can then be included within our projects. This is also known as an assembly.

How Assembly can be used for Better Code Management and Maintenance
As we use Componentization(refer above description), if a code change require you need to change the component code compile it, so you do not require to change all the reference of code for minor change. For better understanding understand the example of component given below.


Types of Assemblies in .Net

  • Static and Dynamic Assemblies
  • Single-File and Multifile Assemblies
  • Private and Shared Assemblies
  • Satellite and Resource-only Assemblies

What is Static Assembly?
A static assembly is created when you compile the program using any of the .NET language compilers. A static assembly contains the types, interfaces, and various resources required by the assembly. A static assembly is stored on the hard disk in the form of a portable executable (.exe or .dll) file. In a simple term, when you compile through VS.Net it generates files which are physically stored on the disk, this files are called static assembly.


What is Dynamic Assembly?
Assemblies which are created and execute on the fly are called dynamic assembly. You can create dynamic assembly through System.Reflection.Emit namespace.

What is Single-File Assembly?
A single-file assembly consists of a single .exe or .dll file.
Example of Single File Assembly
In the example the Shape.dll generated is a Single File Assembly.


What is Multifile Assembly?

  • A multifile assembly is an assembly that can include multiple file, but it should contain atleast one .dll or .exe file.
  • Assembly manifest in multifile assembly can be attached to any assembly file or can be created seperately just the manifest.

What is Satellite Assembly?
Resource-only assemblies are assembly's that stores only resource and no code. example, Image. Resource-only assemblies that store the culture-specific information are known as satellite assemblies.

What is Private Assembly?

  • When you deploy an assembly which can be use by single application, than this assembly is called a private assembly.
  • Private assemblies can be used by only one application they are deployed with.
  • Private assemblies are deployed in the directory where the main application is installed.

What is Shared Assembly?

  • When you deploy an assembly which can be used by several application, than this assembly is called shared assembly.
  • Shared assemblies are stored in a special folder called Global Assembly Cache (GAC), which is accessible by all applications.
  • Shared assemblies must have a strong name. A strong name consists of an assembly name, a version number, a culture, a public key and an optional digital signature.
  • GAC is capable of maintaining multiple copies of an assembly with the same name but different versions.

How to assign strong name to assembly?
You can use strong name tool sn.exe tool to assign strong name to assembly. It provides

  • Name
  • Version
  • Integrity Protection.

How to view content of GAC?
To view content of GAC open explorer and go to c:\Windows\assembly.

Remove Assembly from GAC
To remove all versions of a given Assembly issue:
DotNet> gacutil /u HowDoYouDoSharedAssembly

Native Assembly Vs JIT-Compiled Assembly
Assemblies store code in the MSIL (Microsoft Intermediate Language) Format. When a method is invoked for the first time, the CLR just-in-time (JIT) compiles that method into native machine code. The native code is stored in memory and directly used for subsequent calls to this method. In the JIT compilation mode, a method is slow when it is called for the first time because an additional step of compilation is invoked, but any subsequent calls to that method will run as fast as native code itself.
When you view the GAC, note that some assemblies have their type marked as native images, meaning that these assemblies were precompiled in native code before they were installed in the native image cache. The advangate of using a native image is that even the first call of any method in an assembly will be as fast as its subsequent calls. You too can create a native image for your assembly by using the Native Image Generator Toolngen.exe, which is installed as part of .net framework.

Q . If you have made an application and its using a DLL which is present in GAC. Now for some reason you make second version of the same DLL and put it in GAC. Now which DLL does the application refer? Ok by default it always uses the version by which you have compiled you application in IDE. But you want that it should actually use the older version. 

Ans . In that situation you need to specify “bindingRedirect” in your config file. For instance in the below case “ClassLibraryVersion” has two versions “1.1.1830.10493” and “1.0.1830.10461” from which “1.1.1830.10493” is the recent version. But using the bindingRedirect we can specify saying “1.0.1830.10461” is the new version. So the client will not use “1.1.1830.10493”.


<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="ClassLibraryVersion"
publicKeyToken="b035c4774706cc72" culture="neutral"/>
<bindingRedirect oldVersion"1.1.1830.10493"
newVersion"1.0.1830.10461"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>

 Name the different components of an assembly.

An assembly is a logical unit that is made up of the following four different types of components:
  • Assembly manifest
  • MSIL source code
  • Type metadata
  • Resources
What are the different types of assemblies? Explain them in detail.

The following are the two types of assemblies:
  • Private Assembly - Refers to the assembly that is used by a single application. Private assemblies are kept in a local folder in which the client application has been installed.
  • Public or Shared Assembly - Refers to the assembly that is allowed to be shared by multiple applications. A shared assembly must reside in Global Assembly Cache (GAC) with a strong name assigned to it.
For example, imagine that you have created a DLL containing information about your business logic. This DLL can be used by your client application. In order to run the client application, the DLL must be included in the same folder in which the client application has been installed. This makes the assembly private to your application. Now suppose that the DLL needs to be reused in different applications. Therefore, instead of copying the DLL in every client application folder, it can be placed in the global assembly cache using the GAC tool. These assemblies are called shared assemblies.

 Can one DLL file contain the compiled code of more than one .NET language?

No, a DLL file can contain the compiled code of only one programming language.
What is the maximum number of classes that can be contained in a DLL file?

There is no limit to the maximum number of classes that can be contained in a DLL file.

What is metadata?

An assembly metadata describes every data type and member defined in the code. It stores the description of an assembly, such as name, version, culture, public key of an assembly along with the types exported, other assemblies dependent on this assembly, and security permissions needed to run the application. In addition, it stores the description of types, such as the name, visibility, base class, interfaces implemented, and members, such as methods, fields, properties, events, and nested types.

It also stores attributes. Metadata is stored in binary format. Therefore, metadata of an assembly is sharable among applications that execute on various platforms. It can also be exported to other applications to give information about the services and various features of an application.
What is Assembly Manifest?

Assemblies maintain all their information in a special unit called the manifest. Every assembly has a manifest.

The followings are the contents of an Assembly Manifest:
  • Assembly name - Represents a text string that specifies the assembly's name.
  • Version number - Represents a major and minor version number, as well as a revision and build number. The CL.R makes use of these numbers to enforce version policy.
  • Culture - Represents information of the culture or language, which the assembly supports. An assembly is a container of only resources containing culture- or language-specific information.
  • Strong name information - Represents the public key from the publisher, if a strong name is assigned to an assembly.
  • List of all files in the assembly - Represents a hash of each file contained in the assembly and a file name.
  • Type reference information - Represents the information used at the runtime to map a type reference to the file that contains its declaration and implementation.
  • Information on referenced assemblies - Represents a list of other assemblies that are statically referenced by the assembly. Each reference includes the names of dependent assemblies, assembly metadata (version, culture, operating system, and so on), and public key, if the assembly is strong named.
What is the value of the Copy Local property when you add an assembly in the GAC?

False.
What is Native Image Generator?

The Native Image Generator (Ngen.exe) is a tool that creates a native image from an assembly and stores that image to native image cache on the computer. Whenever, an assembly is run, this native image is automatically used to compile the original assembly. In this way, this tool improves the performance of the managed application by loading and executing an assembly faster.

Note that native images are files that consist of compiled processor-specific machine code. The Ngen.exe tool installs these files on to the local computer.
Name the MSIL Disassembler utility that parses any .NET Framework assembly and shows the information in human readable format

The Ildasm.exe utility.
What is the significance of the Strong Name tool?

The Strong Name utility (sn.exe) helps in creating unique public-private key pair files that are called strong name files and signing assemblies with them. It also allows key management, signature generation, and signature verification.
 How can different versions of private assemblies be used in the same application without a re-build?

You can use different versions of private assemblies in the same application without a re-build by specifying the assembly version in the AssemblyInfo.cs or AssemblyInfo.vb file.
What is Global Assembly Cache (GAC) ?

GAC is a central repository (cache) in a system in which assemblies are registered to share among various applications that execute on local or remote machines. .NET Framework provides the GAC tool (gacutil.exe utility), which is used to view and change the content of GAC of a system. Adding new assemblies to GAC and removing assemblies from GAC are some of the tasks that can be performed by using the gacutil.exe utility. GAC can contain multiple versions of the same .NET assembly. CLR checks GAC for a requested assembly before using information of configuration files.

The
 gacutil.exe /i <assembly name> - is the command that is used to install an assembly in GAC. Users use the Command Prompt of Visual Studio to install an assembly in GAC by using this command.

You can see all the assemblies installed in the GAC using the GAC viewer, which is located at the
 <WinDrive>:<WinDir>\assembly directory, where <WinDir> is windows in Windows XP or windows in Windows Vista or WinNT in Windows 2000. Apart from the list of assemblies, the assembly viewer also shows relevant information, such as the global assembly name, version, culture, and the public key token.
Where is the information regarding the version of the assembly stored?

Information for the version of assembly is stored inside the assembly manifest.
Discuss the concept of strong names.

Whenever, an assembly is deployed in GAC to make it shared, a strong name needs to be assigned to it for its unique identification. A strong name contains an assembly's complete identity - the assembly name, version number, and culture information of an assembly. A public key and a digital signature, generated over the assembly, are also contained in a strong name. A strong name makes an assembly identical in GAC.
 Which utility allows you to reference an assembly in an application?

An assembly can be referenced by using the gacutil.exe utility with the /r option. The /r option requires a reference type, a reference ID, and a description.

What is the difference between .EXE and .DLL files?

EXE
1.    It is an executable file, which can be run independently.
2.    EXE is an out-process component, which means that it runs in a separate process.
3.    It cannot be reused in an application.
4.    It has a main function.

DLL
1.    It is Dynamic Link Library that is used as a part of EXE or other DLLs. It cannot be run independently.
2.    It runs in the application process memory, so it is called as in-process component.
3.    It can be reused in an application.

4.    It does not have a main function.






No comments:

Post a Comment