Sunday 20 April 2014

How to use Deployed Assembly in GAC ? or Assembly Resolution



When we give a reference of some dll in our project and  build  the project that referenced dll gets copied in the bin folder of our project, and when we run our project then CLR searches the referenced dll as follows :-


  1. CLR searches the GAC only if the referenced dll is strongly typed.
  2. If assembly is not found in GAC then CLR searches the location in app.config (or web.config) file.
  3. If dll is not specified in config also then CLR searches the dll in the bin folder of the application where dlls usually gets copied when we build our project.
  4. If dll is not found in bin folder then application crashes.




So you will see that when you run the project you will not get the dll but your project will run fine because of its getting dll refrenced from GAC.

Note :-  It also improves the performance of your application because the unneccessary copying of dll will not be done while building your application.

Specifying  Dll in app.config file ?

Now if you don’t want to install your dll in GAC then we can create a separate folder of Shared Assemblies in our project and give the reference of dll from that folder in our project. After that select CopyLocal property to False.
Now you need to add the reference path of that dll in our project in the Configuration node of app.config or web.config project.

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="ClassLibrary"  culture="neutral" publicKeyToken="cded344e9c33e53f"/>
        <codeBase version="1.0.0.0" href="FILE:\\C:\Projects\ClassLibrary.dll"/>
      </dependentAssembly>
    </assemblyBinding>
 </runtime>


Saturday 19 April 2014

Assemblies in C#



What is Assembly

Assembly is a basic building block of .Net framework application. It is basically a compiled code which can be executed by CLR. An assembly is a collection of types and resources that are built to work together and form logical unit of functionality. Assembly can be a DLL or exe depending upon the project which we chose.

Assemblies are basically of two types:-
  1. Private Assembly
  2. Shared Assembly

Private Assembly

It is an assembly which is being used by a single application only. Suppose we have a project in which we refer a dll so when we build that project that dll will copied into the bin folder of our project. That dll becomes private assembly within our project. Generally the dlls which are meant for a specific project are used as private assemblies.

  
Shared Assembly

Assemblies which can be used in more than one project are known to be shared assembly. Shared assemblies are generally installed in GAC. Assemblies which are installed in GAC are made available to all the dot net applications on that machine. 

However there are two more types of assemblies in dot net that are Satellite Assembly and Shared Assembly.  

 GAC

GAC stands for Global Assembly Cache. It is a memory which is used to store the assemblies which are meant to be used by various applications. Every computer which has CLR installed must have GAC. GAC is a location which can be seen at the path below :-
 C:\Windows\assembly” 
for .net applications with framework upto 3.5. For higher frameworks like 4,4.5 GAC can be seen at “C:\Windows\Microsoft.NET\assembly\GAC_MSIL”. 

How to Install assembly in GAC ?

For installing assembly in GAC we first need to generate strong names for the assembly because only strong name assembly gets installed in the GAC. All other assemblies are known as weak name assemblies and they cannot be stored in GAC. For generating a strong name assembly we first create a console application type project. In that solution we will add one more project(Class Library) .  





In class library project I have created a Class Student that contains a method which returns a string. Now I will add reference of class library in my Console application project and will use this method. After Building the project you will see that ClassLibrary DLL is present there(It will come when we build our project). This DLL is a Private Assembly now. 


Generating Strong Name

To make our assembly strongly named we need to generate a unique key value pair, for that we need to open the visual studio command prompt. We can open that by going through all programs -> Visual Studio 2012(Any version which you have) -> Visual Studio Tools -> Developer Command Prompt for VS and open that as run as administrator.


Then type the command sn –k “Path where you want that key will get generated”
  

This will generate the unique key pair which we will use in our application. Now move to visual studio Class library Project and open AssemblyInfo.cs file which is residing in the Properties folder of your project. In the bottom of that file add a new attribute which refer to the key pair file which we have just created.


Now build your Class Library project and make sure it should build successfully. If it builds then it means that your ClassLibrary dll has become strongly typed. If you want to check that then open visual studio command prompt, copy the path of bin-> Debug folder of your ClassLibrary project. Now paste the path in command prompt using cd command (Refer to the below  screenshot). Now type the command sn –T “ClassLibrary.dll”. After typing this command you will get public key token which indicates that your assembly is Strongly Named and it is ready to install in GAC.




Now to install assembly in cache type the following command in the command prom
gacutil  –i “Name of  the Class library”  



Now Assembly is successfully installed on the system. If you want to see that you can see that in the location “C:\Windows\Microsoft.NET\assembly\GAC_MSIL”.

Note:- If you are using framework greater than 4.0 then you can search the above given path but if you are using framework lesser than 4.0 than your assembly will get stored installed at “C:\Windows\assembly”.


Installing assembly in GAC also solves the problem of DLL Hell