抛出AppDomain异常

时间:2012-12-08 19:48:40

标签: c# appdomain .net-assembly

我发现了很多类似的问题,但找不到任何解决方案。

我有以下代码:

    string file = "c:\\abc.dll";
    AppDomainSetup ads = new AppDomainSetup();
    ads.PrivateBinPath = Path.GetDirectoryName(file);
    AppDomain ad2 = AppDomain.CreateDomain("AD2", null, ads);
    ProxyDomain proxy = (ProxyDomain)ad2.CreateInstanceAndUnwrap(typeof(ProxyDomain).Assembly.FullName, typeof(ProxyDomain).FullName); 

Assembly asm = proxy.GetAssembly(file); // exception File.IO assembly not found is thrown here after succesfully running the funktion GetAssembly.


  public class ProxyDomain : MarshalByRefObject
  {
     public Assembly GetAssembly(string assemblyPath)
     {
        try
        {
           Assembly asm = Assembly.LoadFile(assemblyPath);
           //...asm is initialized correctly, I can see everything with debugger
           return asm;
        }
        catch
        {
           return null;
        }
     }
  }

然后我的GetAssembly funktion返回其他类型的最有趣的事情,即使我的自定义可序列化类,一切都很好。有人知道我错过了什么吗?或者只是不可能将加载的程序集返回到另一个域?

谢谢

1 个答案:

答案 0 :(得分:0)

我想File.IO位于主应用程序的bin目录中?如果是这样,你的abc.dll将不知道在哪里找到它(除非你的主应用程序也位于C:\\)。

你需要做一个

  1. BindAppDomain.AssemblyResolve事件并手动加载引用的dll
  2. 更改AppDomainSetup的基目录(.NET知道查找dll的地方之一)
  3. File.IO安装到GAC.NET知道要查找dll的另一个地方)
  4. File.IO的位置添加到AppDomainSetup的私有探测路径(这是.NET将尝试查找dll的另一个地点)。
相关问题