Assembly in .Net
Compiling projects results in executable files, generally known as assemblies. Assemblies are the building blocks of every .net applications. Assemblies are loaded into memory when they required. Primary unit of deployment is known as assembly. Whenever we compile the source code of any language of .net, then respective compiler comes into picture and converting into MSIL, just after compilation compiler generate a file known as assembly which is either dll or exe file.
Assembly consist of four parts:-
1. Manifest - contains type, version, public key token number, culture
2. Metadata - contains the structure of classes
3. MSIL - contains managed code
Types of Assembly
1. Private Assembly
The assembly which is not registered in GAC (Global Assembly in Catch) known as private assembly. Whenever you call a private assembly in your project, then its generate a local copy for that particular application.
2. Public / Share / Global Assembly
The assembly which is registered in GAC is known as public assembly. Whenever you call a public assembly in your project, then it does not generate a local copy for that project.
3. Satellite Assembly
Satellite assemblies are used to build multilingual applications. It provides the culture information to build an application supportive for multiple languages. This assembly is used for language resources. We can change the culture of assembly. This is used for localization and globalization. Its generate a local copy of dll.
All versioning is done only on assemblies with strong name. The versioning information is stored in the assembly's manifest along with other identity information. Each assembly has a version number as part of its identity. The version number is physically represented as a four part string with following format:
<major version> .< minor version >.< build number >.< revision >
major.minor.build.revision -->> For example: 18.104.22.168
these are 128 bit version number
1. Revision - The revision is normally a random number used to differentiate two version that have same build number. Normally you set a new number every time you check in and your project is built.
2. Build - The build number is a number that normally increase every day. This is done normally every night by your nightly build process.
3. Minor - The minor number is increased with every public release of your project.
4. Major - The major number is increased every time when you have major release of your product, either by changing existing features or rewriting the entire application.
We have two ways to change the version of an assembly:
1. Go to property click assembly information
2. Open assemblyinfo.cs file and write.
A strong name is the unique name used to identify the assembly in GAC. When we build strong name it will generate public and private key pairs. To build strong name we use a tool known as sn.exe. syntax for build strong name of as assembly, sn.exe -k key filename.snl
A strong name is composed of a set of related data:
1. The Friendly name
2. The version
3. The culture
4. The public key token
Friendly name is the name of assembly file without the extension. for example ajax.dll, in which ajax is friendly name.
version explained above topic
The culture represents the culture of the assembly. The culture is used when you want to localize, your application for different markets.
The private key is used to digitally sign the assembly whereas the public key is used to verify the signature. The public token is a 64- bit hash of the assembly public key. An assembly that is not digitally signed can have the public key token set to null. To sign an assembly, you need a pair of public/private keys.
You can generate those keys in two ways:
Side by Side Versioning
Strong naming would have only little values without the power of running side by side different version of the assembly. Side by side versioning works only with strong assemblies because it requires them to be deployed into GAC.
Adding Assemblies in GAC
Only Signed assemblies can be place in GAC. GAC is an assembly repository that acts as a cache for libraries.
1. The simplest way to install a shared assembly into GAC is to drag & drop the assembly to C:\Windows\Assembly using Windows Explores.
copy pasting of the assembly into GAC into GAC windows will not work.
You must literally drag & drop the ajax.dll from one windows into GAC windows, while dragging & dropping an assembly is just fine for local testing.
2. Use an installer that do this, this is the preferred way. Using this, you can install as well as uninstall as assembly using Windows Installer.
3. Using gacutil.exe
Run the following commands:-
gacutil.exe [option] [assembly name] [assembly path] [assemblylist file]
/i --> Install/add an assembly to the GAC
/u --> Uninstall/remove an assembly to the GAC
/l --> List all the assemblies into GAC
gacutil -i ajax.dll
gacutil.exe -u ajax.dll
If an assembly is already loaded in the memory, the CLR does not load it again.
Assembly signature is verified before it is installed on the GAC. so, when the assembly will be loaded it skips verification and start up time of your application will be fast.