What is an assembly?
- An Assembly is a logical unit of code
- Assembly physically exist as DLLs or EXEs
- One assembly can contain one or more files
- The constituent files can include any file types like image files, text files etc. along with DLLs or EXEs
- When you compile your source code by default the exe/dll generated is actually an assembly
- Unless your code is bundled as assembly it can not be used in any other application
- When you talk about version of a component you are actually talking about version of the assembly to which the component belongs.
- Every assembly file contains information about itself. This information is called as Assembly Manifest.
What is assembly manifest?
- Assembly manifest is a data structure which stores information about an assembly
- This information is stored within the assembly file(DLL/EXE) itself
- The information includes version information, list of constituent files etc.
What is private and shared assembly?
The assembly which is used only by a single application is called as private assembly. Suppose you created a DLL which encapsulates your business logic. This DLL will be used by your client application only and not by any other application. In order to run the application properly your DLL must reside in the same folder in which the client application is installed. Thus the assembly is private to your application.
Suppose that you are creating a general purpose DLL which provides functionality which will be used by variety of applications. Now, instead of each client application having its own copy of DLL you can place the DLL in 'global assembly cache'. Such assemblies are called as shared assemblies.
What is Global Assembly Cache?
Global assembly cache is nothing but a special disk folder where all the shared assemblies will be kept. It is located under
How assemblies avoid DLL Hell?
As stated earlier most of the assemblies are private. Hence each client application refers assemblies from its own installation folder. So, even though there are multiple versions of same assembly they will not conflict with each other. Consider following example:
How do I create shared assemblies?
Following steps are involved in creating shared assemblies:
- Create your DLL/EXE source code
- Generate unique assembly name using SN utility
- Sign your DLL/EXE with the private key by modifying Assembly Info file
- Compile your DLL/EXE
- Place the resultant DLL/EXE in global assembly cache using
How do I create unique assembly name?
Microsoft now uses a public-private key pair to uniquely identify an assembly. These keys are generated using a utility called SN.exe (SN stands for shared name). The most common syntax of is :
Sn -k mykeyfile.key
Where k represents that we want to generate a key and the file name followed is the file in which the keys will be stored.
How do I sign my DLL/EXE?
Before placing the assembly into shared cache you need to sign it using the keys we just generated. You mention the signing information in a special file called Assembly Info. Open the file from VS.NET solution explorer and change it to include following lines :
[Assembly: AssemblyKeyFile ("file_path")]
Now recompile the project and the assembly will be signed for you.
Note: You can also supply the key file information during command line compilation via /a.keyfile switch.
How do I place the assembly in shared cache?
Microsoft has provided a utility called AL.exe to actually place your assembly in shared cache. AL /i: my_dll.dll Now your dll will be placed at proper location by the utility.