首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为AppDomain.AssemblyResolve设置处理程序时出现异常

为AppDomain.AssemblyResolve设置处理程序时出现异常
EN

Stack Overflow用户
提问于 2009-08-03 13:23:48
回答 2查看 4.1K关注 0票数 1

创建一个新的应用程序域,设置assemblyResolve处理程序,你总是会得到一个异常,说‘程序集当前正在执行的程序集找不到’

怎么回事?代码如下

代码语言:javascript
复制
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‘片段

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2009-09-03 16:11:13

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

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

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

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

票数 1
EN

Stack Overflow用户

发布于 2009-08-24 14:18:38

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

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1222335

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档