【问题标题】:After Handling AssemblyResolve Event Still getting Exception FileNotFound处理 AssemblyResolve 事件后仍然出现异常 FileNotFound
【发布时间】:2013-10-08 14:08:21
【问题描述】:

我已经处理了AssemblyResolve-事件,但我仍然得到了FileNotFoundException。我已经订阅了类型初始化器中的事件,并在Main-method 中调用了Assembly.LoadFrom 方法:

class Program
{
    static Program()
    {
        AppDomain.CurrentDomain.AssemblyResolve+=new ResolveEventHandler(DeployAssemblyHandler);
    }

    static void Main(string[] args)
    {      
        try
        {
            System.Reflection.Assembly asm=Assembly.LoadFrom("AxInterop.SHDocVw.dll");            
        }
        catch(Exception)
        {
        }
    }

    public static System.Reflection.Assembly DeployAssemblyHandler(object sender,ResolveEventArgs args)
    {            
        Assembly asm = null;
        string asmName = new AssemblyName(args.Name).Name;

        string deployAssemblyDirPath = ""; // Common.AppUtil.InstallDir + AppUtil.DeployedAssemblyDir;

        string[] deployDirectories = Directory.GetDirectories(deployAssemblyDirPath);            

        foreach(string deploy in deployDirectories)
        {
            try
            {
                asm = Assembly.LoadFrom(deploy + "\\" + asmName);
                break;
            }
            catch (Exception ex) { }
        }                   
        return asm;
    }
}

【问题讨论】:

  • 文件AxInterop.SHDocVw.dllbin目录下吗?

标签: c# .net assemblyresolve


【解决方案1】:

我遇到了类似的问题,最后使用新的 AppDomain AND (important!) 设置 PrivateBinPath 属性。另一个 AppDomain 的好处是,如果不再需要它,您可以卸载程序集。 (我的)示例代码是:

  public class ProxyDomain : MarshalByRefObject
  {
      public bool TestAssembly(string assemblyPath)
      {
         Assembly testDLL = Assembly.LoadFile(assemblyPath);
         //do whatever you need 

         return true;
      }
   }

 AppDomainSetup ads = new AppDomainSetup();
 ads.PrivateBinPath = Path.GetDirectoryName("C:\\some.dll");
 AppDomain ad2 = AppDomain.CreateDomain("AD2", null, ads);
 ProxyDomain proxy = (ProxyDomain)ad2.CreateInstanceAndUnwrap(typeof(ProxyDomain).Assembly.FullName, typeof(ProxyDomain).FullName);
 bool isTdll = proxy.TestAssembly("C:\\some.dll");
 AppDomain.Unload(ad2);

编辑:根据您的评论,您只是在寻找错误的事件处理程序。在您的情况下,您应该使用 AppDomain.UnhandledException 事件,因为 AssemblyResolve 事件有不同的目的。

【讨论】:

  • 我的问题不是加载程序集,但我必须检查是否在 loadfrom failed 场景中调用了程序集解析事件
  • 让我解释一下我的场景,我必须从我当前的工作目录加载一个程序集,但如果它不存在,则 loadfrom 必须引发程序集解析事件,我的处理程序将在其他地方搜索它...... ...
  • 如果接受的答案解决了你的问题,那就没关系,否则我会尝试使用 AppDomain.UnhandledException。使用我的代码,你甚至不需要你的例程,因为总会找到 dll
【解决方案2】:

问题在于AssemblyName.Name 属性返回的程序集名称没有扩展名。您需要将".dll" 添加到路径的末尾。 (警告:如果程序集的扩展名不是 .dll,这显然会中断)

【讨论】:

  • 它永远不会遇到事件尚未引发的代码行,并且在 loadfrom 行时抛出异常 filenotfound ...
  • @RoyaanKhan 您使用的是 Load 还是 LoadFrom?您可能应该使用 Load。
【解决方案3】:

尝试在 Main 方法中使用 Assembly.Load 而不是 Assembly.LoadFromAssembly.LoadFrom 甚至不应该调用您的自定义解析器。

【讨论】:

    【解决方案4】:

    在 bin 文件夹中添加 AxInterop.SHDocVw.dll 它应该可以工作。

    【讨论】:

      猜你喜欢
      • 2020-06-03
      • 1970-01-01
      • 1970-01-01
      • 2018-05-05
      • 2010-11-06
      • 2011-02-02
      • 1970-01-01
      • 1970-01-01
      • 2011-09-01
      相关资源
      最近更新 更多