VB.NET Assemblies

In this article you will learn about the assemblies in VB.NET.
  • 4111

An assembly is a group of running programs or libraries that are deployed as a file or set of files in a single unit for implementation. Assemblies are the basic building blocks for all .NET applications and components. Every assembly contains an assembly manifest that contains descriptive information about the assembly. This information includes the name of the assembly, the files that make up the assembly, dependencies, version information, and permissions for allowing the assembly to run. .NET consists of both private assemblies and shared assemblies.

Private Assemblies

A private assembly is one that is known only to the application(s) in a single directory structure. For example, a component is a private assembly if it is placed in the same directory as the application that uses it. If all of your application depends only upon itself, then all of your assemblies can be made private. Private assemblies require you only to copy all the files to a single directory in order to deploy them.

Shared Assemblies

What happens if you need to share a component between different client applications? In this case you need to use a shared assembly. A shared assembly resides in what is known as the global assembly cache (GAC). The GAC also can be used to view all of the shared assemblies (we'll show you how to do this later in the coming article). All shared assemblies need to be assigned a strong name. A strong name means that the assembly is digitally signed and needs to be opened with a key. The strong name identity consists of the name, version number, and cultural information of the assembly along with the public key and digital signature.

Information Identifying a Shared Assembly


To install an assembly to the GAC, you can use the command line gacutil.exe:

gacutil.exe /i MySharedAssembly.dll

Example of Creating a Strong-Named Assembly

The best way to understand how to create a strong-named component is to go through the motions. First, let's create a simple component called SmileyFace.dll. Go to File?New Project and choose Windows Control Library. Type in the project name SmileyFace. See Figure 26.1.

Creating the SmileyFace Control


Now double-click the Paint event in the Project window of the control and add the code in Listing 26.1.

Listing 26.1: Added Code

    Private Sub UserControl1_Paint(ByVal sender As Object, ByVal e As_            
        Dim g As Graphics = e.Graphics
        g.FillEllipse(Brushes.Yellow, 0, 0, ClientRectangle.Width, ClientRectangle.Height)
        g.DrawEllipse(Pens.Black, 0, 0, ClientRectangle.Width, ClientRectangle.Height)
        g.FillEllipse(Brushes.Black, ClientRectangle.Width / 4, ClientRectangle.Height / 4,
        5, 5)
        g.FillEllipse(Brushes.Black, ClientRectangle.Width * 3 \ 4, ClientRectangle.Height /
        4, 5, 5)
        g.DrawArc(Pens.Black, ClientRectangle.Width / 6, ClientRectangle.Height / 3,
        ClientRectangle.Width * 3 \ 4, ClientRectangle.Width / 2, 25, 135)
    End Sub

This will draw the smiley face in the control. Now open the assembly file AssemblyInfo.vb. Change the assembly information to the following to change the culture and version and assign an encryption key file:

        <Assembly: AssemblyCulture("en")>
        ' culture english
        <Assembly: AssemblyVersion("")>
        ' version 1.1
        <Assembly: AssemblyKeyFile("..\..\keyfile.snk")>

Now when we build the assembly, the DLL will have a strong name based on the public and private key in this file. We can actually view the assembly manifest by using the ildasm.exe utility from the command line in the Debug directory where the DLL is contained:

ildasm SmileyFace.dll

This will bring up the window shown in below image.

ILDASM for Viewing Assemblies


Double-clicking the manifest shows the strong name identity information that we've inserted in our component (public key information, version number, and cultural info).

Manifest Detail Inside the SmileyFace Component


Now we need to add the assembly to the GAC. We can either use the gacutil.exe or simply drag and drop the assembly using Windows Explorer. The GAC is located in the Windows directory under the Assembly subdirectory. If you go to c:\windows\assembly (or c:\winnt\assembly if you use Windows NT or 2000) the Explorer will be transformed into a GAC utility. You can then drag SmileyFace.dll into the GAC from another Explorer window. The results are shown in below image.

Global Assembly Cache in Windows Explorer


Note that the assembly name, version number, culture, and public key are all displayed reflecting the changes we made to the AssemblyInfo.vb file. We can now run sn.exe -v from the command line to verify that we have a strong-named assembly:

sn.exe -v SmileyFace.dll

Running sn.exe will produce the following output:

Assembly 'smileyface.dll' is valid.

Probing for Assemblies

If the assembly is not in the GAC, the runtime uses specific rules to look for it. First, it will look for the assembly in the application base (executing) directory. If it doesn't find it there, it will look for a directory named after the assembly in question and then proceed to look inside that directory. If it can't find it there, and if cultural information is indicated, it will look in a directory with the culture name (e.g., en, de, etc.). If it cannot find the assembly in any of those locations, the runtime asks the Windows Installer to provide the assembly. If none of these actions works, a FileNotFoundException is thrown. As an example, consider our SmileyFace.dll assembly run using c:\MyApp\SmileyFaceTest.exe. In this case, the following locations are probed for the SmileyFace.dll assembly:



Hope this article would have helped you in understanding the assemblies in VB.NET.


More Articles

© 2020 DotNetHeaven. All rights reserved.