Category Archives: C#

What is Delegate, Multicast Delegate & Event?

Before writing about Delegates I would like to introduce you about the problems that we are facing before delegate come in existence.

Heavy Coupling- Heavy coupling between method declaration and definition creates Continue reading

Advertisements

What is GAC & How to handle multiple versions in GAC?

GAC-Global Assembly Cache is where all shared .NET assembly reside.

GAC is used in the following situations-

1. If the assembly has to be shared among several application which are in the same computer.

2. If the assembly has some special security, requirements like only administrator can remove the assembly. If the assembly is private for program then a simple delete of assembly can delete the assembly file.

 

Our all share assembly is located in “c:\WINDOWS\assembly\” where we can also see the version and public key token of assembly.

 

To register any assembly in GAC we have to follow bellow steps-

1. Assign strong name to Assembly

2. Copy the Assembly in another location (like d:\Commondll\) and then register the dll in GAC by 2 ways. First way-

I. Go to Visual studio command prompt

II. Go to d:\Commondll\ (where our Assembly is copied)

III. Just type- gacutil -i common.dll

Second way-

Just drag and drop common.dll file from source to “c:\WINDOWS\assembly\”

 

Note- We can cross check by search the common.dll in “c:\WINDOWS\assembly\”

 

Now to use common.dll registered in GAC we have to add reference in windows application from “d:\Commondll\common.dll” and then use common namespace in windows application and now we can use all methods of common.dll in our windows application.

After compilation we will move the windowapplication.exe to “d:\Commondll\” and we can see the .exe running properly because it’s linked with common.dll register in GAC and reside in the same folder.

To create a new version of .dll file in GAC go to Properties->AssemblyInfo.cs in Common class library and update the assembly version- [assembly: Assemblyversion(“1.2.0.0”)] and then after build the application lets register this assembly in GAC.

To change the pointing dll by any application-

1. Go to Administrative Tools->Microsoft .NET Framework 2.0 Configuration

2. Right click on Applications in the left hand side tree structure and add windowapplication

3. Select windowapplication after it will add to Applications and click on “Managed Configured Assemblies” then “Configure an Assembly”-> “Choose an assembly from the assembly cache”-> click on “Choose Assembly” and then choose Common which has version 1.1.0.0 and click on Finish.

4. As we click on finish we will get new popup which has Binding Policy tab and enter 1.1.0.0 in Requested Version and 1.2.0.0 in New Version. It creates windowapplication.exe.config file which will redirect the control to new version when it calls for old version.

What is delay signing

Delay Signing helps us to prevent our application from our own developers. Because our developer knows everything about our application, they know strong key file, dll name, function name etc so they can easily take the strong key file and can place a fake dll, that’s where Delay Signing comes into picture.

Structure of Strong Key-

When we create a Strong Key there are 2 parts to it-

  1. Public Key
  2. Private Key

So we have to share only Public Strong Key with the internal developers and when we want to deploy the application at that time we have to add the Private Strong Key and do the deployment.

We can understand the whole concept of Delay Signing by a Strong Key creation-

To create a Strong Key we have to open “Visual Studio Command Prompt”

  1. Go to the desired location where you want to create Strong Name Key file. (for example D:\Akash\Study\Projects\Windows Application\Strong Name)
  2. Type- sn -k MyStrongName.snk. This command created a Strong Name Key file in desired location. This key hold both public as well as private key.
  3. In order to extract Public Strong Name Key from above Strong Name Key file Type-

sn -p MyStrongName.snk MyStrongNamePublic.snk

  1. Now we can give the MyStrongNamePublic.snk file to our internal developers for development and store the MyStrongName.snk file in a secured location and will use this at the time of deployment. REMEMBER A POINT when we using delay signing concept we have to check the check box “Delay sign only” at the time of Public Strong Name Kay file assignment.
  2. At the time of deployment we have to assign the secured Strong Name Key file to the dll so to do this we have to go to the location where the secured Strong Name Key file stored, Type-

sn –R ClassLibrary1.dll MyStrongName.snk

What is Garbage Collector, Gen 0, 1 & 2?

Garbage Collector (GC) -Garbage Collector is a feature of CLR which cleans unused Managed objects and reclaims memory. It does not clean unmanaged objects. It’s a background thread which runs continuously.

Generation defines age of the object. There are 3 generations-

1. Gen 0: When application creates fresh objects they are marked as Gen 0.

2. Gen 1: When GC is not able to clear the objects from Gen 0 in first round it moves them to Gen 1 bucket.

3. Gen 2: When GC visits Gen 1 Objects and it is not able to clear the objects from Gen 1, it moves them to Gen 2 bucket.

Generations are created to improve GC performance. GC will spend more time on Gen 0 objects rather than Gen 1 and Gen 2 thus improving performance. More objects is in Gen 0 is good sign rather than more objects in Gen 1/ Gen 2.

We can see Generations of Garbage Collector by creating a windows application in which we will put a button and put the following code in .cs file. We will use CLR Profiler to see the Generations.

private void button1_Click(object sender, EventArgs e)

{

for(int i=0;i<1000000;i++)

{

cls obj=new cls();

}

}

}

public class cls1

{

}

CLRProfiler- CLRProfier is a tool provided by Microsoft.com. To see Generations of Garbage Collector (Gen0, 1, 2).