I have handled the AssemblyResolve
-event but I still get a FileNotFoundException
. I have subscribed to the event in the type initializer and call the Assembly.LoadFrom
method in the Main
-method:
class Program
{
static Program()
{
AppDomain.CurrentDomain.AssemblyResolve+=new ResolveEventHandler(DeployAssemblyHandler);
}
static void Main(string[] args)
{
try
{
System.Reflection.Assembly asm=Assembly.LoadFrom("AxInterop.SHDocVw.dll");
}
catch(Exception)
{
}
}
public static System.Reflection.Assembly DeployAssemblyHandler(object sender,ResolveEventArgs args)
{
Assembly asm = null;
string asmName = new AssemblyName(args.Name).Name;
string deployAssemblyDirPath = ""; // Common.AppUtil.InstallDir + AppUtil.DeployedAssemblyDir;
string[] deployDirectories = Directory.GetDirectories(deployAssemblyDirPath);
foreach(string deploy in deployDirectories)
{
try
{
asm = Assembly.LoadFrom(deploy + "\\" + asmName);
break;
}
catch (Exception ex) { }
}
return asm;
}
}
I had the similar problem and ended with using a new AppDomain AND (important!) setting the PrivateBinPath property. The good thing about another AppDomain is that you can unload the assembly if you do not longer need it. The (my) sample code is:
public class ProxyDomain : MarshalByRefObject
{
public bool TestAssembly(string assemblyPath)
{
Assembly testDLL = Assembly.LoadFile(assemblyPath);
//do whatever you need
return true;
}
}
AppDomainSetup ads = new AppDomainSetup();
ads.PrivateBinPath = Path.GetDirectoryName("C:\\some.dll");
AppDomain ad2 = AppDomain.CreateDomain("AD2", null, ads);
ProxyDomain proxy = (ProxyDomain)ad2.CreateInstanceAndUnwrap(typeof(ProxyDomain).Assembly.FullName, typeof(ProxyDomain).FullName);
bool isTdll = proxy.TestAssembly("C:\\some.dll");
AppDomain.Unload(ad2);
EDIT: based on your comment you are just looking for the wrong event handler. In your case you should use an AppDomain.UnhandledException Event, because AssemblyResolve Event has different purpose.