.NET Assembly Loading

There are two main ways to programmatically load assemblies. You can use the assembly loader or the assembly resolver. The assembly loader uses System.Reflection.Assembly class’ LoadFrom static method. The assembly resolver uses the System.Reflection.Assembly class’ Load static method.

The assembly loader loads a specified file i.e. from a file path like so:

var assembly = System.Reflection.Assembly.LoadFrom(“file://D:/MyFileToLoad.dll”);

The assembly resolver on the other hand takes a four-part assembly name – also called a display name – and then goes through a series of steps to get to the correct file to load using the assembly loader.

The assembly resolver loads a file:

var assembly = System.Reflection.Assembly.Load(“MyFileToLoad, version=1.2.3.4, culture=en-US, publickeytoken=44ab55cd11bb22ce”);

The assembly resolver starts with applying a version policy. It looks like this:

Version policy is applied from version policy files. The first one is application policy located in your applications application configuration file. Then there is a publisher policy located in the global assembly cache (GAC). Finally there is a machine policy located in the machine.config file located in %SystemRoot%\Microsoft.Net\Framework\v?\CONFIG. As you can see from the figure above, each policy output is the next policy input.

A publisher policy is a policy the developer of the component creates, that are stating which versions of a given component are compatible with another. Publisher policies are stored as configuration files in the machine wide GAC. The structure is the same as for app.config or machine.config files.

The publisher policy file must be wrapped in an assembly – as a custom resource – prior to installing in the GAC. It can be done like so:

al.exe /link:policy.config /out:policy.2.0.Foo.dll /keyf:pubpriv.snk /v:2.0.0.0

The out name of the publisher policy wrapped file follows the policy.major.minor.assemblyname.dll naming convention. This means that only one publisher policy file can exist per major.minor version number.

In the preceding example any reference to the Foo assembly, version 2.0, will go through the policy file that now lies in policy.2.0.Foo.dll.

The assembly resolver uses the following steps to find the correct file to load using the assembly loader.

Assembly resolver.

Probing is a search at APPBASE or in APPBASE sub-directories. However only sub directories that are configured in the application configuration file will be probed.

<?xml version=”1.0”?>
<configuration xmlns:asm=”urn:schemas-microsoft-com:asm.v1”
  <runtime>
    <asm:assemblyBinding>
      <asm:probing privateBinPath=”common;bin”/>
    </asm:assemblyBinding>
  </runtime>
</configuration>

application configuration file with probing set

Say our four part assembly name starts with: “Test, culture=neutral”

Say our application folder is in C:\TestApp\Test.exe

Say our probing paths are set as “common;bin”

This culture neutral probing would look in the following locations for a potential CODEBASE:

C:\TestApp\Test.dll
C:\TestApp\Test\Test.dll
C:\TestApp\common\Test.dll
C:\TestApp\common\Test\Test.dll
C:\TestApp\bin\Test.dll
C:\TestApp\bin\Test\Test.dll
C:\TestApp\Test.exe
C:\TestApp\Test\Test.exe
C:\TestApp\common\Test.exe
C:\TestApp\common\Test\Test.exe
C:\TestApp\bin\Test.exe
C:\TestApp\bin\Test\Test.exe

Now Say our four-part assembly name starts with: “Test, culture=en-US”

Say our application folder is in C:\TestApp\Test.exe

Say our probing paths are set as “common;bin”

This culture dependent probing would look in the following locations for a potential CODEBASE:

C:\TestApp\en-US\Test.dll
C:\TestApp\en-US\Test\Test.dll
C:\TestApp\common\en-US\Test.dll
C:\TestApp\common\en-US\Test\Test.dll
C:\TestApp\bin\en-US\Test.dll
C:\TestApp\bin\en-US\Test\Test.dll
C:\TestApp\en-US\Test.exe
C:\TestApp\en-US\Test\Test.exe
C:\TestApp\common\en-US\Test.exe
C:\TestApp\common\en-US\Test\Test.exe
C:\TestApp\bin\en-US\Test.exe
C:\TestApp\bin\en-US\Test\Test.exe

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

%d bloggers like this: