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.

Turn on Internet connection sharing” option missing,Windows 7


This is a crazy bug and the main reason for this blog. The first time you enable internet connection sharing on an ad hoc connection, everything should be fine. And then… this option disappears. Impossible to find it. Impossible to share your internet connection again and without internet connection sharing, your ad hoc network looks useless.


Why this bug ?


Basically, you can only share your internet connection sharing once. That means it is linked to the first ad hoc network you have created and won’t be available unless you remove your first-in-you-life-created ad hoc network. The problem ? This ad hoc network is not shown anywhere ! Thank you Microsoft ! Basically you need to remove something that is not shown anywhere and that you cannot manage. Restarting your PC, looking wherever you want won’t help either. When Microsoft does something bad it does it really bad.


The solution ?


Well, there is a lot of “solutions” on the net but none worked for me. I found my method and wanted to share it (reason for this whole blog).


1.Go to your network settings: “Control Panel” / “Network and Sharing Center” and click on the wonderfully called “Change adapter settings” (why windows cannot just call it “Manage internet connections” is beyond me)


2.You should see 2 important connections here:


– your local connection (the one that enables you to connect to the internet). It is probably in state “Network, Shared”.


– your wireless connection (the one you will be using to give internet access to your other devices.


– you may have plenty of other connections that are not important.


– you will NOT have ad hoc connections (that would be too good to be true).


3.Right click on your local connection and go to Properties. There should be a tab called “Sharing” which is probably activated. Disable it (uncheck the first option “Allow other…”).


4.Disable your wireless connection.


5.Re-enable your wireless connection. As you have unchecked the sharing, when your wireless connection is re-enabled it should not use the internet-sharing-unique-slot.


6.Simply retry to create an ad-hoc connection. The option “Turn on Internet connection sharing” should now appear correctly.

Table Variables in Sql Server 2005


Table variable is almost similar to the temporary table in sql server 2005. Table variables are replacement to temporary tables. The following syntax shows how to define a table variable.

Declare @Customer table ( CustomerID int identity(1,1), CustomerName varchar(100), CompanyName varchar(100), Address varchar(250) )

You can notice the syntax to define a table variable is similar to the syntax to normal table definition. declare keyword is used in place of create keyword. And table name is prefixed with ‘@’ as all tsql variables do.


1.Table variables are stored in memory rather than in database.


2.Querying table variables is very fast as there are no disk reads needed.


3.The scope the table variables is same as other tsql variables. i.e, within statements batch or sproc


4.Table variables cannot be dropped as they are automatically disappeared when they reach out scope


5.As explained above all the data in table variables is stored in server’s memory. So if there is huge data then it is not recommended to use table variables to avoid memory overhead.


6.If the number of records is more than 100 then it is recommended to use temporary tables.To learn more about temporary tables please read this blog post :



How to create temporary table in sql server 2005?

The syntax to create a temporary table is exactly same as the syntax to create a normal table. But the difference is the table name is prefixed with ‘#’ (pound). By looking at the prefix ‘#’ the sql server understands that it is a temporary table. The following code demonstrates how to create a temporary variable.

CREATE TABLE #Customer ( CustomerID int identity(1,1), CustomerName varchar(100), CompanyName varchar(100), Address varchar(250) )

1.It is almost similar to normal table with very few exceptions.


2.The scope of the temp table is the current connection session. It can be accessed from any where in same connection session.


3.This table is automatically dropped when the connection session is closed.


4.Different connection sessions creating temporary table with same name will create different copies of the temporary table with the same name. Actually internally the table name is appended with a unique number to support many connection sessions creating tables with same name. You can notice how the table name is uniquely maintained by sql server by running this query. select * from information_schema.tables where table_name like ‘%customer%’


5.foreign key relations cannot be applied to temp tables.


6.Optionally you may drop the table at the end of it’s use. It is a good practice to drop any temporary table after use. drop table #Customer


When you create a temporary table it will be created in tempdb. At the time of table creation the tempdb is locked and hence there is some overhead involved using this. If there are less than 100 records then using temporary tables is not recommended. In such cases you can use table variables which are stored in memory rather than in database(on disk). To learn more about table variables please read this :