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

发布于 2024-07-30 04:27:04 字数 1020 浏览 3 评论 0原文

创建一个新的应用程序域,设置 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,版本=1.0.0.0,文化=中性,PublicKeyToken=null”[片段]

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]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深空失忆 2024-08-06 04:27:04

您的解析器可能不会在新的 AppDomain 上触发,请尝试将其设置在 AppDomain.CurrentAppDomain 上。

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

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

至于使用 byte[] 加载程序集,这可以解决文件锁定问题,但它不会解决您的问题,我认为看不到 此处

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

捂风挽笑 2024-08-06 04:27:04

您正在尝试加载不在 AppDomain 基本位置下的程序集。 我也从未让 AssemblyResolve 事件对我有用。

我建议将基外程序集加载到字节数组 (System.IO.File.ReadAllBytes) 中,然后将该数组交给 到您新创建的 AppDomain 进行加载

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文