创建一个新的应用程序域,设置assemblyResolve处理程序,你总是会得到一个异常,说‘程序集当前正在执行的程序集找不到’
怎么回事?代码如下
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;
}例外情况是:
System.IO.FileNotFoundException:未能加载文件或程序集'appAdmin,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null‘或其依赖项之一。系统找不到指定的文件。文件名:'appAdmin,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null‘片段
发布于 2009-09-03 16:11:13
您的解析程序可能无法在新的AppDomain上触发,请尝试在AppDomain.CurrentAppDomain上设置它。
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(sandBox_AssemblyResolve);
在sandBox_AssemblyResolve方法中,您可以从您喜欢的任何目录加载程序集,这是从byte[]加载可以发挥作用的地方。
至于使用byte[]加载程序集这修复了文件锁定问题,它不会修复您的问题我不认为看到here
发布于 2009-08-24 14:18:38
您正在尝试加载不在AppDomain的基位置下的程序集。我也从来没有让AssemblyResolve事件为我工作过。
我建议将你的基础外程序集加载到一个字节数组(System.IO.File.ReadAllBytes)中,然后将该数组传递给to your newly created AppDomain to load。
https://stackoverflow.com/questions/1222335
复制相似问题