.Net Assembly Probing : Simple Assembly

Navneet.Net Full Stack Developer

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)
                FullName.NonGAC.FullName.GetFullName("John", "Smith")

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.
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.
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"?>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <probing privatePath="MyDLLs" />

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.


Locating the Assembly through Codebases or Probing

<probing> Element

Assembly Binding Log Viewe

How to load an assembly at runtime that is located in a folder that is not the bin folder of the application
Navneet.Net Full Stack Developer

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.

Get access with a 7-day free trial.
You Belong in the World's Smartest IT Community