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
:\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) end sub end class Compile above code using : vbc sampletest.vb /t:exe /r: 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. ……………. Assembly
Assemblies are form the fundamental unit of deployment, version control, reuse, activation scoping, and security permissions. An assembly provides the common language runtime with the information it needs to be aware of type implementations
Static or Dynamic Assembly
Assemblies can be static or dynamic. Static assemblies can include .NET Framework types (interfaces and classes), as well as resources for the assembly (bitmaps, JPEG files, resource files, and so on). Static assemblies are stored on disk in PE files. You can also use the .NET Framework to create dynamic assemblies, which are run directly from memory and are not saved to disk before execution. You can save dynamic assemblies to disk after they have executed.
Contents of Assembly
In general, a static assembly can consist of four elements: The assembly manifest, which contains assembly metadata. Type metadata. Microsoft intermediate language (MSIL) code that implements the types. A set of resources
Types of Assemblies
1. Private Assembly 2. Public/ Shared Assembly 3. Satellite Assembly
Difference between a Private Assembly and a Shared Assembly
1.Location and visibility: A private assembly is normally used by a single application, and is stored in the application's directory, or a sub-directory beneath. A shared assembly is normally stored in the global assembly cache, which is a repository of assemblies maintained by the .NET runtime. Shared assemblies are usually libraries of code which many applications will find useful, e.g. the .NET framework classes. 2.Versioning: The runtime enforces versioning constraints only on shared assemblies, not on private assemblies
Satellite Assembly
Satellite assemblies are often used to deploy language-specific resources for an application. These language-specific assemblies work in side-by-side execution because the application has a separate product ID for each language and installs satellite assemblies in a language-specific subdirectory for each language ……….
Introduction Each machine on which the Common Language Runtime is installed has a machine-wide code cache. This cache is divided into two components, a download cache and a global assembly cache. The global assembly cache is used to store assemblies meant to be used by several applications on the machine. The only way to deploy an assembly into the global assembly cache is by using an installer designed to work with the global assembly cache, or a tool in the SDK such as al.exe. There are several reasons you would want to install an assembly into the global assembly cache. These include: ·Performance improvements – Performance is improved in several ways for assemblies in the global assembly cache. First, an assembly does not have to go through verification each time it is loaded. Second, if multiple applications are referencing the assembly, the operating system will single instance the assembly and loading speed will significantly increase. Finally, the runtime can find the assemblies faster. ·Integrity checking – When an assembly is added to the global assembly cache, integrity checks are performed on all files of the assembly based on the hash value and hash algorithm specified for that file in the assembly manifest. ·File security – Only users with Administrator priviledges can delete files from the global assembly cache. ·Versioning – Multiple copies of assemblies with the same name but different version information can be maintained in the global assembly cache. ·Automatic pickup of QFEs – When the runtime locates an assembly through probing or through the codebase, it will check the global assembly cache for any QFEs of the assembly (providing Automatic QFE policy has not been turned off in the configuration file). If there is a version of the assembly in the cache that has a higher build and/or revision number, this assembly is loaded instead of the one found by probing or examining the codebase. ·Additional search location – If the runtime does not find an assembly via probing or through the codebase, it will check the global assembly cache for an assembly that matches the assembly request. Assemblies deployed in the global assembly cache must have a shared name. When an assembly is added to the global assembly cache, integrity checks are performed on all the files that make up the assembly. Since the assemblies in the global assembly cache can be accessed directly from the file system, the cache performs these integrity checks to ensure that an assembly has not been tampered with (e.g. a file changed but the manifest version stayed the same). Only users with Administrator privileges on the machine on which the cache resides can perform management tasks on the global assembly cache such as installing or uninstalling assemblies. How to create Shared Assembly?
The steps involved in creating shared assemblies is as follows ·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
Creating 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. Signing 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. 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. ……….. Assemblies can be private or shared. Private assemblies are available only to clients in the same directory structure as the assembly; shared assemblies are available to any local COM application. All assemblies and type libraries must be registered in the Windows registry so COM clients can use the managed types transparently. Private Assemblies You deploy an application into an application directory and subdirectories if it is to be private. The following illustration shows the Loanlib.dll installed in two separate application directories. To run a private assembly from the Visual Basic 6.0 development environment, the assembly must be in the application directory of the Visual Basic executable (Vb6.exe). Directory structure and registry entries for private deployment
Shared Assemblies You install the assemblies for an application into the global assembly cache if they are to be shared. All shared assemblies must be strong-named (signed by the publisher). Any COM application that references a type in the assembly encounters Mscoree.dll, which in turn locates the assembly. Use the Global Assembly Cache tool (Gacutil.exe) to add an assembly to the global assembly cache. For example Copy Code gacutil /i LoanLib.dll ………. The global assembly cache is a computer-wide code cache provided by the .NET Framework that is used to store assemblies that need to be shared by several applications on the computer. In order to be installed to the global assembly cache, the assembly must be strong-named. To install an assembly to the global assembly cache, add the assembly or the project output group for the assembly to the Global Assembly Cache folder in the File System Editor. The Global Assembly Cache folder is unlike the other folders in the File System Editor. It has no properties that are settable, and you cannot create shortcuts to the folder or to assemblies in the folder.