When CLR loader cannot find an assembly via the standard probing, CLR loader will give you a chance to provide the assembly, by firing AssemblyResolve event. You can subscribe to this event, and provide the assembly when necessary.

 

Now what if your event handler depends on another assembly, and that assembly cannot be found by CLR loader? You can imagine that CLR loader will call your AssemblyResolve event handler again and again, resulting in a stack overflow exception.

 

Let’s try some trivial example:

 

C:\assemblyresolve>more unmet.cs

namespace Unmet

{

    public class Unmet

    {

    }

}

 

C:\assemblyresolve>more resolve.cs

using System;

using System.Reflection;

using Unmet;

 

public class ResolveHook

{

    static Assembly MyResolveEventHandler(object sender, ResolveEventArgs args)

    {

        new Unmet.Unmet();

        return null;

    }

 

    public static void Main(String[] args)

    {

        try {

            AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(M

yResolveEventHandler);

            Assembly.Load("NonExistant");

        }

        catch (Exception e) {

            Console.WriteLine("Exception: {0} : {1}", e.GetType().ToString(), e.

Message);

        }

    }

}

C:\assemblyresolve>csc /t:library unmet.cs

Microsoft (R) Visual C# .NET Compiler version 8.00.40607.16

for Microsoft (R) Windows (R) .NET Framework version 2.0.40607

Copyright (C) Microsoft Corporation 2001-2003. All rights reserved.

 

 

C:\assemblyresolve>csc /r:unmet.dll resolve.cs

Microsoft (R) Visual C# .NET Compiler version 8.00.40607.16

for Microsoft (R) Windows (R) .NET Framework version 2.0.40607

Copyright (C) Microsoft Corporation 2001-2003. All rights reserved.

 

 

C:\assemblyresolve>resolve

Exception: System.IO.FileNotFoundException : File or assembly name 'NonExistant'

, or one of its dependencies, was not found.

 

C:\assemblyresolve>move unmet.dll unmet.dll.bak

 

C:\assemblyresolve>resolve

Exception: System.StackOverflowException : Exception of type 'System.StackOverflowException' was thrown.

 

Moral of the story: When CLR loader depends on you, you better not depend on someone else.