Assembly in Asp.Net

An assembly is a logical unit of code. It physically exists as DLLs or EXEs. One assembly can contain one or more files like image files, text files etc. along with DLLs or EXEs.
Assemblies in the .Net platform are a solution to a problem that has been around as long as libraries. The problem knows as “DLL Hell”, can be caused by one application overwriting a shared library of another application, usually with a different version.
Imagine you write an application with a shared library and then years later create a new application with an updated library. In order not to break old programs, what do you do with the new library? You could give it a different name, store it in a different location, or overwrite the old ones?
.Net assemblies overcome these issues by storing information about themselves, rather than having that information stored in central location (i.e. system registry). This information is called as Assembly Manifest.
Assembly manifest

  • Assembly manifest is a data structure which stores information about an assembly.
  • This information is stored within the assembly file itself.
  • The information includes version information, list of constituent files etc.

What is in the Assembly?
The assembly contains the intermediate code, resources, and the metadata for itself. We can have a look inside the assembly using the ildasm (Intermediate Language Disassembler) tool that comes as part of Visual Studio. To access it you need to open the Visual Studio Command Prompt and type ildasm.exe. To open the Visual Studio Command Prompt goes to:
Start>All Program>Microsoft Visual Studio 2010>Visual Studio Tools>Visual Studio Command Prompt (2010)

Visual Studio Command Prompt
Figure 1: Visual Studio Command Prompt

This will launch a window application that you can use to explore any .Net application. Once launched, you can open any .Net application (File>Open) and view information within the file, including the manifest, types, classes, namespaces, methods and source code.
Visual Studio Command Prompt
Figure 2: Intermediate Language Disassembler

Assembly Types
You can create two types of ASP.Net Assemblies: Private Assemblies and Shared Assemblies.
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 shared assemblies.
Creating a Shared Assembly
The procedure for creating a shared assembly is the same as for a private assembly, except that there are a few extra steps involved before you can use it. All shared assemblies need to be strongly named before they can be installed into the global assembly cache. Strongly named assemblies ensure that the assembly has unique manifest consisting of versioning information and public key.
Steps to create shared assemblies:

  • Create your DLL source code.
  • Generate unique assembly name using SN utility.
  • Sign your DLL with the private key by modifying AssemblyInfo file.
  • Compile your DLL.
  • Place the resultant DLL in global assembly cache using GACUtil utility.

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.
If you are using .Net framework 4.0 then the folder will be : :\WinNT\Microsoft.Net\Assembly.
How do I create unique assembly name?
Microsoft now uses a public-private key pair to uniquely identify an assembly. To generate this cryptographic key pair, use the SN Tool. Go to Visual studio command prompt and use the following syntax to generate a key file.

sn -k “[DriveLetter]:\[DirectoryToPlaceKeyFile]\[KeyFileName].snk”
e.g. sn -k “C:\GACKey\GACkey.snk”

How do I sign my Assembly?
A key is generated, but it is not yet associated with the assembly of the project. To create this association, double-click the AssemblyInfo.vb file in Visual Studio .NET Solution Explorer. This file has the list of assembly attributes that are included by default when a project is created in Visual Studio .NET.
Modify the AssemblyInfo.vb by adding the following AssemblyKeyFile attribute:

[Assembly: AssemblyKeyFile(“C:\\GACKey\\GACKey.snk”)]

How do I place the assembly in shared cache?
You can install the .dll file by using the Gacutil tool or by dragging the .dll file to the appropriate folder. If you use the Gacutil tool, you can use a command that resembles the following:

gacutil -I “[DriveLetter]:\[DirectoryOfDllFile]\[dll File]”

To drag the file, open two instances of Windows Explorer. In one instance, find the location of the .dll file output for your console project. In the other instance, find WinNT\Assembly. Then, drag your .dll file to the Assembly folder.
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 SharedAssembly. We will also create a key file named sample.snk. 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 “Hello World”.

Namespace SharedAssembly
  Public Class Test
    Public Function Helloworld() As String
      Return “Hello World”
    End Function
  End Class
End Namespace

Visual Studio Command Prompt
Figure 3: VB.Net Class File

Step 2: Generate a key file
To generate the key file use the following command at visual studio command prompt:

SN –k c:\sample.snk

Visual Studio Command Prompt
Figure 4 : Generate Key File

Step 3: Sign your component with the key file
To sign the component add the following AssemblyKeyFile attribute in AssemblyInfo.vb file:

[Assembly: AssemblyKeyFile(“C:\\sample.snk”)]

Visual Studio Command Prompt
Figue 5 : AssemblyInfo.vb

Step 4: Host the signed assembly in Global Assembly Cache
To install the assembly in Global Assembly Cache we will use GACUtil Tool. Go to Visual studio command prompt, use the following code :

gacutil –i D:\Test\\Assembly\SharedAssembly

Visual Studio Command Prompt
Figure 6: GACUtil.exe

After hosting the assembly just goes to WINNT\Assembly folder and you will find your assembly listed there.

State Management with Context.Items,

Passing values between pages is a common task in every web application.There are many options for state management including Session, Cookies, QueryString etc. Each of these have their strength and weakness and each is designed for different circumstances, it just depends of your requirement. This article will focus on the use of context.items collection to pass variable between different pages.

Assume we have a ecommerce site, the page “ProductList” shows the list of products.When I click on a product, it will redirect the user to “ProductDescription”. How do I tell “ProductDescription” which product to show ?

One possible approach is to use the QueryString and pass the ProductId to the “ProductDescription” page as shown in listing 1 and 2.


Listing 1: “ProductList” using the QueryString

Dim PrdId as Integer=Request.QueryString(“PrdId”)

Listing 2: “ProductDescription” using the QueryString

The above approach is totally acceptable. There are several disadvantages of this approach, some of which are as follows:

  • The QueryString is exposed with the URL meaning sensitive data may be revealed.
  • The length of the QueryString is generally limited (e.g. 256 bytes in IE).
  • The QueryString can only contain character data (no objects).
  • Response.Redirect requires a round trip from server to client to server.

A better approach is to store the Product Id in Context.Items collection, then use server.transfer to redirect to the “ProductDescription” as shown in listing 3 and 4.


Listing 3: “ProductList” using the Context.Items collection

Dim PrdId as Integer=Context.Items.Item(“PrdId”)

Listing 4: “ProductDescription” using the Context.Items collection

Server.Transfer performs a server-side redirect that is transparent to the user, meaning there is no need of a round trip.The Context.Items collection is also capable of storing objects instead of just plain character data.

It is important to keep in mind that values stored in the Context.Items collection only last for the current request. If I were to do a postback on the “ProductDescription” page, the Context item “Id” would be lost. Most of the time this is convienient since we only need to store that value between pages and no longer.

Viewstate collection is the solution to retain the values between postbacks.