Professional Documents
Culture Documents
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 <drive>:\WinNT\Assembly folder. 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 : You created assembly Assembly1 You also created a client application which uses Assembly1 say Client1 You installed the client in C:\MyApp1 and also placed Assembly1 in this folder After some days you changed Assembly1 You now created another application Client2 which uses this changed Assembly1 You installed Client2 in C:\MyApp2 and also placed changed Assembly1 in this folder
Since both the clients are referring to their own versions of Assembly1 everything goes on smoothly Now consider the case when you develop assembly that is shared one. In this case it is important to know how assemblies are versioned. All assemblies has a version number in the form: major.minor.build.revision If you change the original assembly the changed version will be considered compatible with existing one if the major and minor versions of both the assemblies match. When the client application requests assembly the requested version number is matched against available versions and the version matching major and minor version numbers and having most latest build and revision number are supplied. 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 AssemblyInfo file Compile your DLL/EXE Place the resultant DLL/EXE in global assembly cache using AL utility
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 AssemblyInfo. 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.
Hands On... Now, that we have understood the basics of assemblies let us apply our knowledge by developing a simple shared assembly. In this example we will create a VB.NET component called SampleGAC ( GAC stands for Global Assembly Cache). We will also create a key file named sample.key. We will sign our component with this key file and place it in Global Assembly Cache. Step 1 : Creating our sample component Here is the code for the component. It just includes one method which returns a string.
imports system namespace BAJComponents public class Sample public function GetData() as string return "hello world" end function end class end namespace
Step 2 : Generate a key file To generate the key file issue following command at command prompt.
sn -k sample.key
This will generate the key file in the same folder Step 3 : Sign your component with the key Now, wee will sign the assembly with the key file we just created.
vbc sampleGAC.vb /t:library /a.keyfile:sample.key
Step 4 : Host the signed assembly in Global Assembly Cache We will use AL utility to place the assembly in Global Assembly Cache.
AL /i:sampleGAC.dll
After hosting the assembly just go to WINNT\Assembly folder and you will find your assembly listed there. Note how the assembly folder is treated differently that normal folders. Step 5 : Test that our assembly works Now, we will create a sample client application which uses our shared assembly. Just create a sample code as listed below :
imports system imports BAJComponents public class SampleTest shared sub main() dim x as new sample dim s as string="x".getdata() console.writeline(s)
Now, copy the resulting EXE in any other folder and run it. It will display "Hello World" indicating that it is using our shared assembly.
you can also add a strong key to a project's dll. for instance if Edraw is your project in VS 2008 and its generating Edraw.dll then you can goto project properties (i.e. right click on project-> properties) then "Signing" tab, and after that check "Sign the assembly" check box. A drop down box below "Sign the assembly" checkbox will get enable "choose a strong name key file" select New in it will ask you for file name pass Edraw.snk to it. if you want the file to be password protected pass the password information to the same dialog box otherwise uncheck the "protect my key file with a password" checkbox.
This article describes how to generate a strong name for an assembly and to install a .dll file in the Global Assembly Cache. The Global Assembly Cache (GAC) enables you to share assemblies across numerous applications. The GAC is automatically installed with the .NET runtime. Components are typically stored in C:\WINNT\Assembly. In order to install an assembly in the GAC, you must give the assembly a strong (that is, globally unique) name. Actually, it is not a name but a cryptographic hash key, or signature. The strong name ensures correct component versioning, which helps to prevent components that have the same name from conflicting with each other or being incorrectly used by a consuming application. Back to the top
Requirements
The following items describe the recommended hardware, software, network infrastructure, skills and knowledge, and service packs that you need: Administrator rights to the computer on which the shared assembly is being installed Prior knowledge required: General familiarity with assemblies in .NET General familiarity with the use of tools from the command-line prompt Back to the top
Verification
1. Start Windows Explorer. 2. Navigate to C:\WINNT\assembly. 3. Find GAC in the list of installed .dll files.
An assembly, or a dynamic linking library (DLL), is linked to your program at run time. To demonstrate building and using a DLL, consider the following scenario: MathLibrary.DLL: The library file that contains the methods to be called at run time. In this example, the DLL contains two methods, Add and Multiply. Add: The source file that contains the method Add. It returns the sum of its parameters. The class AddClass that contains the method Add is a member of the namespace UtilityMethods. Mult: The source code that contains the method Multiply. It returns the product of its parameters. The class MultiplyClass that contains the method Multiply is also a member of the namespace UtilityMethods. TestCode: The file that contains the Main method. It uses the methods in the DLL file to calculate the sum and the product of the run-time arguments. his file contains the algorithm that uses the DLL methods, Add and Multiply. It starts with parsing the arguments entered from the command line, num1 and num2. Then it calculates the sum by using the Add method on the AddClass class, and the product by using the Multiply method on the MultiplyClass class. Notice that the using directive (Imports in Visual Basic) at the beginning of the file enables you to use the unqualified class names to reference the DLL methods at compile time, as follows:
MultiplyClass.Multiply(num1, num2); UtilityMethods.MultiplyClass.Multiply(num1, num2);
To run the program, enter the name of the EXE file, followed by two numbers, as follows: TestCode 1234 5678 To build the file MathLibrary.DLL, compile the two files Add and Mult by using the following command line.
csc /target:library /out:MathLibrary.DLL Add.cs Mult.cs
The /target:library compiler option tells the compiler to output a DLL instead of an EXE file. The /out compiler option followed by a file name is used to specify the DLL file name. Otherwise, the compiler uses the first file (Add.cs) as the name of the DLL. To build the executable file, TestCode.exe, use the following command line:
csc /out:TestCode.exe /reference:MathLibrary.DLL TestCode.cs
The /out compiler option tells the compiler to output an EXE file and specifies the name of the output
file (TestCode.exe). This compiler option is optional. The /reference compiler option specifies the DLL file or files that this program uses.
// File: Add.cs namespace UtilityMethods { public class AddClass { public static long Add(long i, long j) { return (i + j); } } } ... // File: Mult.cs namespace UtilityMethods { public class MultiplyClass { public static long Multiply(long x, long y) { return (x * y); } } } ... // File: TestCode.cs using UtilityMethods; class TestCode { static void Main(string[] args) { System.Console.WriteLine("Calling methods from MathLibrary.DLL:"); if (args.Length != 2) { System.Console.WriteLine("Usage: TestCode <num1> <num2>"); return; } long num1 = long.Parse(args[0]); long num2 = long.Parse(args[1]);
long sum = AddClass.Add(num1, num2); long product = MultiplyClass.Multiply(num1, num2); System.Console.WriteLine("{0} + {1} = {2}", num1, num2, sum); System.Console.WriteLine("{0} * {1} = {2}", num1, num2, product);
} } /* Output (assuming 1234 and 5678 are entered as command-line arguments): Calling methods from MathLibrary.DLL: 1234 + 5678 = 6912 1234 * 5678 = 7006652 */