为 AppDomain.AssemblyResolve 设置处理程序时出现异常
创建一个新的应用程序域,设置 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的解析器可能不会在新的 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
您正在尝试加载不在 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.