tags:

views:

679

answers:

2

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]

A: 

You're trying to load assemblies that aren't under the AppDomain's base location. I've never had the AssemblyResolve event work for me, either.

I'd suggest loading your out-of-base assembly into a byte array (System.IO.File.ReadAllBytes) and then hand that array to your newly created AppDomain to load.

Will
+1  A: 

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

learnerplates