无法将自定义DLL加载到新的AppDomain中。显示未找到的文件或其依赖项

时间:2012-11-08 06:54:30

标签: appdomain

我无法将自定义dll加载到我的新appDomain。     AppDomainSetup appSetup = new AppDomainSetup()       {             ApplicationName =“PluginsDomain”,             ApplicationBase = AppDomain.CurrentDomain.BaseDirectory,             PrivateBinPath = @“插件”,             DisallowBindingRedirects = false,             DisallowCodeDownload = true,             ConfigurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile       };

   AppDomain domain = AppDomain.CreateDomain("MyPlugin", null, appSetup);

    byte[] bytes = File.ReadAllBytes(@"C:\Users\testUser\Documents\Visual Studio 
                   2012\Projects\BaseClass\BaseClass\bin\helpClass\HelperClass.dll");

   Assembly assm= domain.Load(bytes);
   Type type=assm.assm.GetExportedTypes()[0];

   Object testObj=(Object)appDomain.CreateInstanceAndUnwrap(assm.FullName, type.FullName);

  ///Here is the problem When I try to compile it show error.

1 个答案:

答案 0 :(得分:0)

您需要在远程域(“MyPlugin”)中为AssemblyResolve实现一个事件侦听器,该侦听器返回所述程序集(在您的情况下为HelperClass.dll)

您是否尝试将HelperClass用作“代理”?如果可能,我建议您创建一个“代理”(继承自MarshalByRefObj),然后创建CreateInstanceAndUnwrap到您的远程域。然后在“Proxy”中实现AssemblyResolve,并通过“Proxy”加载动态程序集。

//Listenner to AssemblyResolve
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
    if(args.Name==assm.Fullname)
    {
         return assm;
    }
    else
    {
         return null;
    }
}

我之前写过a post about AppDomain and implementing a "Proxy"