简体   繁体   English

抛出AppDomain异常

[英]AppDomain exception is thrown

I've found a lot of similar questions but couldn't find any solution. 我发现了很多类似的问题,但是找不到任何解决方案。

I have following code: 我有以下代码:

    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;
        }
     }
  }

The most Interesting thing that then my GetAssembly funktion returns some other type, even my custom serializable class, everything is fine. 最有趣的是,然后我的GetAssembly函数返回其他某种类型,即使是我的自定义可序列化类,也一切正常。 Does someone know what I'm missing? 有人知道我在想什么吗? Or It's just impossible to return loaded assembly to another domain? 还是不可能将已加载的程序集返回到另一个域?

Thank you 谢谢

I imagine File.IO is sitting in your main application's bin directory? 我想File.IO坐在主应用程序的bin目录中? If so, your abc.dll will not know where to find it (unless your main application is also sitting in C:\\\\ ). 如果是这样,您的abc.dll将不知道在哪里可以找到它(除非您的主应用程序也位于C:\\\\ )。

You need to do one of 您需要执行以下一项操作

  1. Bind to the AppDomain.AssemblyResolve event and manually load the referenced dll BindAppDomain.AssemblyResolve事件并手动加载引用的dll
  2. Change the AppDomainSetup 's base directory (which is one of the places .NET knows to look for dlls) 更改AppDomainSetup的基本目录( .NET知道其查找dll的位置之一)
  3. Install File.IO to the GAC (which is another one of the places .NET knows to look for dlls) File.IO安装到GAC (这是.NET知道查找dll的另一个地方)
  4. Add the location of File.IO to your AppDomainSetup's private probing path (which is another one of the places .NET will try to look for dlls). File.IO的位置添加到AppDomainSetup的专用探测路径(这是.NET将尝试查找dll的另一个位置)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM