无法将自定义Dll加载到新的AppDomain中。显示文件不存在或其依赖关系不存在

问题描述 投票:1回答:1

我无法将自定义dll加载到我的新的 appDomain.

AppDomainSetup appSetup = new AppDomainSetup()
{
        ApplicationName = "PluginsDomain",
        ApplicationBase = AppDomain.CurrentDomain.BaseDirectory,
        PrivateBinPath = @"Plugin",
        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.

appdomain
1个回答
0
投票

你需要在你的远程域("MyPlugin")中为AssemblyResolve实现一个事件监听器,以返回所述的汇编(在你的例子中是HelperClass.dll)。

你是想把HelperClass当作 "代理 "使用吗?如果可能的话,我建议你创建一个 "代理"(继承自MarshalByRefObj),然后创建InstanceAndUnwrap到你的远程域。然后在 "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;
    }
}

我以前写过 一篇关于AppDomain和实现 "代理 "的文章。

© www.soinside.com 2019 - 2024. All rights reserved.