本文介绍了装载/卸载组件在不同的AppDomain的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在运行时加载的程序集来执行的方法。现在我想的方法调用之后卸载这些加载的程序集。我知道我需要一个新的AppDomain这样我就可以卸载库。但在这里,问题就出现了。

要加载的程序集是在我的插件框架插件。他们没有入口点的。我所知道的是,它们含有某些类型的其中实现给定的接口。旧的,非AppDomain- code看起来像这样(略缩短):

 尝试
{
    字符串路径= Path.GetFullPath(C:\\ library.dll);
    AppDomain.CurrentDomain.AssemblyResolve + = CurrentDomain_AssemblyResolve;
    大会ASM = Assembly.LoadFrom(路径);
    键入[]类型= asm.GetExportedTypes();
    的foreach(在类型T型)
    {
        如果((t.GetInterface(IStarter)=空)及!&放大器;!t.IsAbstract)
        {
            反对TEMPOBJ = Activator.CreateInstance(T);
            MethodInfo的信息= t.GetMethod(GetParameters);
            如果(信息!= NULL)
            {
                返回info.Invoke(TEMPOBJ,NULL)作为字符串;
            }
        }
    }
}
赶上(异常前)
{
    MessageBox.Show(的String.Format(该死的'{0}',ex.Message),异常,MessageBoxButtons.OK,MessageBoxIcon.Error);
}私人大会CurrentDomain_AssemblyResolve(对象发件人,ResolveEventArgs参数)
{
    如果(args.Name.StartsWith(MyProject.View,))
    {
        字符串路径= Path.GetFullPath(C:\\ view.dll));
        返回Assembly.LoadFrom(路径);
    }
    返回null;
}

现在我希望他们能在自己的AppDomain中加载:

 尝试
{
    字符串路径= Path.GetFullPath(C:\\ library.dll);
    AppDomain中域= AppDomain.CreateDomain(TempDomain);
    domain.AssemblyResolve + = CurrentDomain_AssemblyResolve; // 1.例外在这里!
    domain.ExecuteAssembly(路径); // 2.异常在这里!
    domain.CreateInstanceFrom(...); // 3,我不知道,该类型的命名方式。
    domain.Load(...); // 4.我不知道,该组件的命名方式。
    domain.DoCallBack(...); // 5.例外在这里!
    // ...
}
赶上(异常前)
{
    MessageBox.Show(的String.Format(该死的'{0}',ex.Message),异常,MessageBoxButtons.OK,MessageBoxIcon.Error);
}

正如你所看到的,我已经把5例。


  1. 如果我设置了事件处理程序,我得到一个异常的组件(它是一个管理控制台(程序mmc.exe)管理单元。无法找到/加载。


  2. ExecuteAssembly没有找到一个切入点(当然,是没有的)。


  3. 我不知道该类型的命名方式。如何通过接口加载?


  4. 类似3.如何得到一个程序集的名称?


  5. 同样的错误在1。


我想问题可能是同治控制台好歹我只是不知道我在做什么错。任何帮助是AP preciated。

更新1

我现在已经使用贴代理解决方案尝试。

 的AppDomain域= AppDomain.CreateDomain(TempDomain);
InstanceProxy代理= domain.CreateInstanceAndUnwrap(Assembly.GetAssembly(
    。typeof运算(InstanceProxy))全名的typeof(InstanceProxy)的ToString())作为InstanceProxy;
如果(代理!= NULL)
{
    proxy.LoadAssembly(路径);
}
AppDomain.Unload(域);公共类InstanceProxy:MarshalByRefObject的
{
    公共无效LoadAssembly(字符串路径)
    {
        AppDomain.CurrentDomain.AssemblyResolve + = CurrentDomain_AssemblyResolve;
        大会ASM = Assembly.LoadFrom(路径);
        键入[]类型= asm.GetExportedTypes();
        // ...往上看...
    }
}

这也不起作用。当试图创建代理对象,我得到一个异常:

The file in the error message is the on loaded into mmc (the SnapIn). Any idea how to fix this error? AppDomain.AssemblyResolve is not called (neither in the old or new domain).

UPDATE 2

I have now tried the solution with the AppDomainSetup. Now, the exception has changed to:

Any idea?

解决方案

Try this:

namespace SeperateAppDomainTest
{
    class Program
    {
        static void Main(string[] args)
        {
            LoadAssembly();
        }

        public static void LoadAssembly()
        {
            string pathToDll = Assembly.GetExecutingAssembly().CodeBase;
            AppDomainSetup domainSetup = new AppDomainSetup { PrivateBinPath = pathToDll };
            var newDomain = AppDomain.CreateDomain("FooBar", null, domainSetup);
            ProxyClass c = (ProxyClass)(newDomain.CreateInstanceFromAndUnwrap(pathToDll, typeof(ProxyClass).FullName));
            Console.WriteLine(c == null);

            Console.ReadKey(true);
        }
    }

    public class ProxyClass : MarshalByRefObject { }

这篇关于装载/卸载组件在不同的AppDomain的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 06:30
查看更多