Assembly in Dotnet – Part1

Posted: February 3, 2011 in Uncategorized

Assembly in Dotnet – Part1

An assembly in dotnet world is nothing but a DLL or an exe. It is basic building block of any Dotnet application. Assembly contains
metadata and manifest information. The reason for the emergence of assembly concept was to overcome the common “DLL Hell” problem in COM. The assembly contains all the code, resources, metadata and even version information
. Metadata contains the details of every “type” inside the assembly. In addition to metadata,assemblies also have a special file called Manifest. It contains information about the current version of the assembly, culture information, public key token if available and other related information.

There is tool called ildasm which can used to view  the assembly information. There is also a Dotnet reflector which can be used to dig into assembly details.

Functions of a assembly :

  1. It contains code that the common language runtime executes. It is a managed environment
  2. It forms a security boundary. An assembly is the unit at which permissions are requested and granted
  3. It is the unit at which side-by-side execution is supported.
  4. It forms a deployment unit. When an application starts, only the assemblies that the applicationinitially calls must be present. Other assemblies, such as localization resources or assemblies containing utility classes,can be retrieved on demand
  5. It forms a version boundary. The assembly is the smallest versionable unit in the common language runtime; all types and resources in the same assembly are versioned as a unit.

There are in all 3 different types of assemblies :

  • 1. Private Assembly
  • 2. Shared or Strong named assembly
  • 3. Satellite assembly

This article focuses only on private assembly and few other assembly concepts. Private assembly is a managed standalone executable or a class library file.

§ Creating a simple assembly and referencing other assembly to it

§ Assembly probing

Creating a simple assembly and referencing other assembly to it

I have a created an assembly called “AssemblyHost” and added reference to “Test” assembly. I am calling method “Add” defined
within Test assembly’s class.

Test
Assembly

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace Test

{

public class MyClass

{

public static int Add(int i,int j)

{

return (i + j);

}

}

}

AssemblyHost assembly

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Test;

namespace AssemblyHost
{
    class Program
    {
        static void Main(string[] args)
        {
            int j = MyClass.Add(5, 7);
            Console.WriteLine(j.ToString());
            Console.ReadLine();
        }
    }
}

Output

2. Assembly Probing – Dotnet runtime searches for the assembly in the following path

§

  • Look for the dll or exe under the Application directory (generally bin folder) or AppBase – Example shown above works in this fashion
  • §
  • Look for the assembly folder under the Application directory (generally bin/<assemblyname>) and try to find dll or exe .

Suppose I create one more project “Test1” as seen below :

When I try to load the new assembly without referencing, I get an error. However, if copy and paste a folder “Test1” under my application directory (bin/Debug) and copy the dll..it works

Application Directory – AssemblyHost/bin/Debug

Folder Path – AssemblyHost/bin/Debug/Test

Dll Path – Folder Path/Test.dll

§
Look for probing path defined under configuration file

I have to define my application config file as seen below to search for dll or exes to be loaded into my running assembly. The below configuration searches within MyLib folder inside my Application Directory and searches for all dlls.

<?xml version=1.0 encoding=utf-8 ?>

<configuration>

<runtime>

<assemblyBinding xmlns=urn:schemas-microsoft-com:asm.v1>

<probing privatePath=MyLib/>

</assemblyBinding>

</runtime>

</configuration>

GAC(Global Assembly Cache)

If the private probing fails, then the assembly is searched from within GAC. I will cover GAC in the next part of this article.

Till then happy reading.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s