Tuesday, April 17, 2007

ClassLibrary: Creating dll files

As opposed to running a C# application as a Console application, we can define it as a ClassLibrary and include it within the other programs we are gonna implement in the future.

The main differences of a console application is as follows:



  • Console: The compiled file's extension is .exe so it is executable.
  • ClassLibrary: The compiled file's extension is .dll thus it is NOT executable by itself. However, it is well suited for inclusion by other programs.

ClassLibraries may be used in two ways:

  1. Private
  2. Public

for Private ClassLibraries:
Does it happen when we SIGN and explicitly select the .dll file in Visual Studio, under Project properties??

As for the public ClassLibraries:
The .dll file may be published to a global folder under Windows directory. So, any application can use it.


This process can be done as follows:
First of all, the project must be created as a ClassLibrary in Visual Studio.
Then implement the program codes just like I did in InterestCalculation program in the previous post. Compile the program. This will produce the .dll file.
Then go to VS Command Prompt and run this: > sn -k c:\easy.snk
You will get: > Key pair written to c:\easy.snk

  1. Next thing to do is to use .NET GLOBAL ASSEMBLY CACHE utility.
  2. In the command prompt, type: > gacutil -i C:\{Location of the .dll file}\InterestCalculation.dll
  3. This won't work: You'll get the following error: > Failure adding assembly to the cache: Attempt to install an assembly without a strong name.
  4. The reason was because we haven't digitally signed the project of ClassLibrary. This digital signature is a randomly generated key specific to each of the persons. This is what we did in the 1st step above.
  5. Go to Project Properties (Right-click in Solution Explorer) and switch to SIGNING TAB.
  6. Enable checkbox "Sign the Assembly." Choose a strong name key file. Browse and select the key file [C:\easy.snk in this example].
  7. Now re-do the 3rd step above.
  • This procedure required a minor fix in our case. In Visual Studio 2003, we had to correct the line [assembly: AssemblyKeyFile("")] as [assembly: AssemblyKeyFile(@"C:\easy.snk")]
  • I have Visual C# 2005 Express in my computer. Unfortunately, the above correction was NOT successful and now I am not able to install the assembly to global directory. If anybody has a idea about this, please post it to my blog. Thanks!

Overview:

The Global Assembly Cache is so useful to share the previously written program codes. This is a PUBLIC method of sharing the codes among applications. So, what is the PRIVATE method to accomplish this??

Let's assume that two developers Gerry and Jerry are team members for software development. They are responsible for implementing their own part. However, by mistake, they have used SAME namespace names in the implementation! What will happen during compilation of the project??

The Strong Name Tool (SN.exe) enables us to sign our assemblies with strong names. For the example above, even though Gerry and Jerry used same namespaces by mistake, the digital signing of the application will prevent any compile errors and enable each of the developers to use even the same names for namespaces and classes.

3 comments:

dotneteasy said...

OK guys, I was now able to install the assembly to the cache. I have deleted the .dll's and re-built the project. It gives the following message:


Microsoft (R) .NET Global Assembly Cache Utility. Version 2.0.50727.42
Copyright (c) Microsoft Corporation. All rights reserved.

Assembly successfully added to the cache

shruti said...

Assembly An assembly is the basic unit of deployment. It is a partially compiled code library which can be deployed and used.

The windows called its executables as exe. In .Net framework they are called portable executables (assemblies).
they are same as windows exe except the code is in MSIL which is then converted to machine dependent code BY CLR at runtime.

shruti said...

private assembly is used by a specific application and it is not signed..
hence it cant be used globally means publicly by any application.

they are private to your application