.Net Assembly Probing : Simple Assembly

Navneet HegdeNavneet (navneethegde)
CERTIFIED EXPERT
Published:

Probing Assembly:
Simple Assembly Probing (a .NET assembly without strong name)

Whenever the .NET common language runtime (CLR) tries to locate an assembly, it performs certain steps, including probing. To begin learning this concept, this article will start with an example application that does not use strong-named assemblies. I will discuss strong-named assembly probing in a subsequent next article. Therefore, let's begin with a console application named AssemblyResolutionDemo.
using System;
                      
                      namespace AssemblyResolutionDemo
                      {
                          class Program
                          {
                              static void Main(string[] args)
                              {
                                  Console.WriteLine(
                                      FullName.NonGAC.FullName.GetFullName("John", "Smith")
                                  );
                                  Console.WriteLine(
                                      typeof(FullName.NonGAC.FullName).Assembly.CodeBase
                                  );
                                  Console.Read();
                              }
                          }
                      }

Open in new window



As the code example reflects, AssemblyResolutionDemo references  a project/assembly named FullName.NonGAC.
using System;
                      
                      namespace FullName.NonGAC
                      {
                          public class FullName
                          {
                              public static string GetFullName(string firstName, string lastName)
                              {
                                  return string.Format("{0}, {1}", firstName, lastName);
                              }
                          }
                      }

Open in new window


Notice that the GetFullName function returns a formatted string, which we are accessing in the main application. The assembly's codebase outputs the file path of the loaded assembly, making it easier for us to validate the location from where the CLR loads the assembly.

1. The CLR will check the applications’ base directory first.

When the application runs, the CLR checks the bin directory of the application, loading the assembly if it exists.
 Img003
2. If the assembly does not exist in the bin directory, the CLR checks for a subdirectory with the assembly name (e.g., bin\FullName.NonGAC).

To test this step, set the "Copy Local" property of the FullName.NonGAC assembly in the AssemblyResolutionDemo project to false. Therefore, the DLL file will not be in the bin directory. Subsequently, create a subdirectory of the bin folder called FullName.NonGAC with the associated assembly inside.

Using the Assembly Binding Log Viewer (fuslogvw.exe), a tool available via the Visual Studio command prompt, we can check the assembly probing process via the log file as we run the application again.
Img004Log file reflects that the CLR first looks at the bin folder, then probed the folder with same name as the referenced DLL. It finds and loads the assembly.

Ok what if it doesn’t find it in here?
Img005The CLR exhausts all its available options. Remember we haven’t defined anything in the config file yet; therefore, the CLR only has two options for probing for the DLL. Hence, it throws an error—a FileNotFoundException.
Img006
3. If neither of the aforementioned cases exists, the CLR checks the app.config file for additional subdirectories to probe.

Now we consider that our DLL exists in a subdirectory of the bin folder named MyDLLs. The CLR will not probe this directory unless we define it as a probing path in the configuration file.
<?xml version="1.0"?>
                      <configuration>
                        <runtime>
                          <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
                            <probing privatePath="MyDLLs" />
                          </assemblyBinding>
                        </runtime>
                      </configuration>

Open in new window

Note: one can define multiple folder paths using a semi-colon (;).
<probing privatePath="MyDLLs; DLLs;" />

Open in new window


With the proper app.config, the CLR finds and loads the DLL.
Img008Notice this time the DLL is loaded from the bin/MyDLLs folder.
Img009The above instruction to locate and to load assembly is called private-path probing.

4. Assemblies outside the application's base directory.

Referenced assemblies outside the application's root directory must have strong names and must either be installed in the global assembly cache or specified using the <codeBase> element.
MSDN


References:

Locating the Assembly through Codebases or Probing
http://msdn.microsoft.com/en-us/library/15hyw9x3.aspx 

<probing> Element
http://msdn.microsoft.com/en-us/library/823z9h8w(v=vs.110).aspx

Assembly Binding Log Viewe
http://msdn.microsoft.com/en-us/library/e74a18c4(v=vs.110).aspx

How to load an assembly at runtime that is located in a folder that is not the bin folder of the application
http://support.microsoft.com/kb/837908
0
6,318 Views
Navneet HegdeNavneet (navneethegde)
CERTIFIED EXPERT

Comments (0)

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.