TechBubbles Microsoft Technology BLOG

Assembly binding in .NET Framework

The CLR is responsible for locating and binding the assemblies in the code. In Order to work assembly binding efficiently you need to follow some best practices.

Assembly Naming 

Always use fully specified assembly names. An assembly name is a name given to unique identity. Two assemblies representing the same identity can have the same name with different versions.

Assemblies are mostly loaded by it’s name and keeping the names consist makes it easier for the loader to find the assembly. A fully qualified assembly consist of four fields:

    •     Name of the assembly
    •     The version
    •     The culture
    •     The public key token

Partial Binding

A partial bind occurs only when the user specifies the part of the assembly identity.

Example: Assembly.Load(“SampleAssembly”);

In the above case user failed to specify the other three fields of the identity. This is what we called as partial bind.

Avoid Partial Binds

Since the binder does not have the complete information about assembly which can leads to nondeterministic Binder behavior.

In the case of Assembly.LoadWithPartialName() , the binder simply tries to load and if it fails then picks up the highest version of the assembly in GAC.

If you need to load a particular assembly multiple times or do not wish to hard-code assembly versions into your strings. You can specify the qualified name in the application configuration file in <qualifyAssembly> element then specify the partial reference in Assembly.Load()

using system;
using system.Reflection;

class SampleAssembly
{
  public static void Main()
  {
    try
    {
      Assembly.Load("SampleAssembly");
      
    }

    catch(Exception e)
     {
         Console.WriteLine(e.ToString());
     }
  }
}

Binding Context

Loader Contexts plays an important role in the Assembly binding. Loader context is logical part in application domain that holds the assemblies.

They fall in to three loader contexts

  1. Load Context  all assemblies that are in GAC and Application base are loaded using Assembly.Load() will be loaded into Load Context.
  2. LoadFrom Context If you are loading an assembly that fall in outside the application path and then assembly would not have been found in the Load Context. In this case Assembly is loaded in the LoadFrom Context.
  3. Neither Context If you are attempting to load an assembly using Assembly.LoadFile(), Assembly.Load(byte[]) or Reflection.Emit those are loaded into the Neither context.

AssemblyResolve

The CLR follows a series of steps to locate and bind the assembly. When the assembly cannot be located then binder raises an AssemblyResolve event.

Source: MSDN magazine May 2009 issue

About the author

Kalyan Bandarupalli

My name is kalyan, I am a software architect and builds the applications using Microsoft .NET technologies. Here I am trying to share what I feel and what I think with whoever comes along wandering to Internet home of mine.I hope that this page and its contents will speak for me and that is the reason I am not going to say anything specially about my self here.

Add Comment

TechBubbles Microsoft Technology BLOG

Follow me

Archives

Tag Cloud