C# – Exception setting up handler for AppDomain.AssemblyResolve

appdomaincnet

Create a new appdomain, setup the assemblyResolve handler and
you always get an exception saying 'assembly [current executing assembly] not found'

what gives ? code is below

string _fileName = @"c:\temp\abc123.dll";

AppDomain sandBox = AppDomain.CreateDomain("sandbox");

sandBox.AssemblyResolve += new ResolveEventHandler(sandBox_AssemblyResolve); 
// the line generates the exception !

System.Reflection.Assembly asm = sandBox.Load(System.Reflection.AssemblyName
                                     .GetAssemblyName(fileName).FullName);

foreach (System.Reflection.AssemblyName ar in asm.GetReferencedAssemblies())
    dbgWrite("Ref:  " + ar.FullName );


System.Reflection.Assembly sandBox_AssemblyResolve
  (object sender, ResolveEventArgs e)
{

    System.Reflection.Assembly asm = 
        System.Reflection.Assembly.LoadFrom(_fileName);
    return asm;

}

exception is:

System.IO.FileNotFoundException: Could not load file or assembly 'appAdmin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified. File name: 'appAdmin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' [snip]

Best Answer

Your resolver may not fire on your new AppDomain, try setting it on the AppDomain.CurrentAppDomain instead.

AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(sandBox_AssemblyResolve);

In the sandBox_AssemblyResolve method you can load the assembly up from whatever directories you like, this is where the load from a byte[] may come into play.

As for the loading of an Assembly using byte[] this fixes file locking issues, it won't fix what ails you I don't think see here