Assemblies An assembly is a unit of code for deployment,versioning and security that comes in the form of a DLL (generally).file Assemblies are used when reusability of code is necessary in different applications. Suppose that you are having a set of functions(f1(),f2(),f3()) which are required in more than one project, then instead of writing the functions in all those projects, create a DLL file with these functions. Now we can just refer this DLL file and can make use of the required functions available in it. Prevously in VB 6.0 and others, we used COM (ActiveX technologies) to achieve this functionality. But since COM is binary coded, it can be used only in Windows Also inheritance is not supported and we faced the versioning problem (DLL HELL) with COM We can overcome the above drawbacks of COM with Assemblies. An assembly can be either EXE or DLL , mostly it will be a DLL file. An assembly is a collection of Namespaces which in turn is a collection of Classes. Assembly consists of a) Manifest - Self Description of Assembly.It consists of the required DLL’s information. b) Type MetaData – This will provide the namespace name, class name and methods description. c) MSIL Code – the compiler will convert the source code to CLR understandable code called as Microsoft Intermediate Langage.
An assembly can be either of these 2 types 1. Private Assembly / Folder-Specific Assembly 2. Shared Assembly / Machine-Wide Assembly. Private Assembly: If the assembly is a Private assembly then, the application within that folder( i.e., the folder in which the dll exists) can only access that assembly. Suppose that there is a folder “Dir1” which contains 2 applications – App1 and App2 and an assembly ‘Assembly1.dll’ D:\Dir - App1 - App2 - Assembly1.DLL D:\Dir2 -App3
Now App1,App2 can access Assembly1.dll but it cannot be accessed by App3 since it is stored in different folder- Dir2
When an application, say App1 is executed, the runtime should load the DLL’s and other required files of the application. Since App1 requires ‘Assembly1.DLL’, runtime will refer to application folder & browses for ‘Assembl1.DLL’. This is called as probing.
Steps for creating a Private Assembly : 1. Open a console library Name : Assembly1 Add the following code to it: Public class Math Public Shared Function sum(a as byte, b as byte) as short Return(a+b) End Sub End Class 2 Build the application. We get Assembly1.dll 3. Create a Client Application Open a Windows Application Name : AppClient Now we should refer to the assembly which we created. Therefore, Go to , Solution Explorer References Add Reference Under .Net tab, select “assembly1.dll” Place a Button ( Name: Sum) on the form and in the code window add this code
Imports Assembly1 Public Class Form1 Inherits ……… Private suv Sum_Click(…) Msgbox(Math.sum(10,20)) End Sub End Class
3. Execute the application
When ever we add reference to the assembly, it will copy the dll file into our application folder. When several applications are using same dll, then when references are created for each & every application with the dll, a copy of the dll will be placed in all those application folders. Therefore, more memory will be consumed. To aoid this, use Shared Assemblies.
Shared Assemblies: Several applications through out the machine can access the shared assembly. Therefore, these shared assemblies will be placed in a centralized location from which all the applications can access them.This centralized location is called as GAC. When all the assemblies are placed in one location, there might be a name conflict i.e., more than 1 assembly with the same name. There won’t be a problem if both the assemblies are having the same name but differ either in parameters or other things. And for this we can use the concept of Strong Name – unique identity for the assembly. With this we can maintain the uniqueness of assemblies in GAC. Strong Name Consists of Name Public Key Culture Version The public key for the file can be generated by the utility called as sn.exe . C:App1\> sn -keyfile1.snk This creates a public key & stores it in a keyfile. The compiler reads this keyfile and writes into the dll which is called as signing the assembly.Now this assembly will be called as Strong named assembly.Now this can be placed into GAC. Drag & drop the assembly into GAC.
Eg: Open the project “Assembly1.dll” ( which we created for private assembly) change the name of the assembly to “SharedAssembly1” Go to Solution Explorer, Assemblyinfo.vb ….. Add the following line in this file:
Now build the assembly Now place the assembly into GAC by dragging & dropping or else by copying it into the folder c:\winNT\Assembly.
Client Application: Open a Windows Application Name: ClientApp Go to Solution Explorer, Add Reference Browse “SharedAssembly1.dll” Place a button(Sum) on the form
In Code window, Private sub Sum_Click(…) Msgbox(sharedassembly.math.sum(10,20)) End Sub
Finally, Run the application.
|
| Author: Aravind Dhakshinamoorthi 06 Oct 2004 | Member Level: Bronze Points : 0 |
gr8 job done !!!
|
| Author: S.Vijay Kumar 12 Oct 2004 | Member Level: Bronze Points : 0 |
Article is very clear and good. U did'nt explain how to create sn(sn -k and Installing in the GAC(gacutil -i
|
| Author: V.Raguraman 30 Nov 2004 | Member Level: Bronze Points : 0 |
This article is very useful to beginners.Realy u have done a Good job for .Net Developers.
|