【问题标题】:Exception setting up handler for AppDomain.AssemblyResolve为 AppDomain.AssemblyResolve 设置异常处理程序
【发布时间】:2010-11-16 09:05:16
【问题描述】:

创建一个新的 appdomain,设置 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,版本=1.0.0.0,文化=中性,PublicKeyToken=null”或其依赖项之一。该系统找不到指定的文件。文件名:'appAdmin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' [snip]

【问题讨论】:

  • 这不是一个连贯的问题。一开始您不会显示 sandBox_AssemblyResolve 的内容。现在你认为实际结果应该是什么,也不是完整的 statcktrace
  • 现在清楚了吗?错误是设置事件处理程序=> Assembly.AssemblyResolve += new ....其余的甚至没有机会执行!这就是为什么我没有早点复制它!

标签: c# .net appdomain


【解决方案1】:

您的解析器可能无法在您的新 AppDomain 上触发,请尝试在 AppDomain.CurrentAppDomain 上设置它。

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

在 sandBox_AssemblyResolve 方法中,您可以从您喜欢的任何目录加载程序集,这就是从 byte[] 加载可能发挥作用的地方。

至于使用 byte[] 加载程序集,这解决了文件锁定问题,它不会解决你的问题我不认为看到here

【讨论】:

    【解决方案2】:

    您正在尝试加载不在 AppDomain 基本位置下的程序集。我也从来没有让 AssemblyResolve 事件为我工作过。

    我建议将您的基础程序集加载到一个字节数组 (System.IO.File.ReadAllBytes) 中,然后将该数组交给 to your newly created AppDomain to load

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-04
      相关资源
      最近更新 更多