在不同的AppDomain与依赖负载组件 [英] Load assemblies with dependencies in a different AppDomain

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

问题描述

我的目标是让2中的文件夹之间缺少相关检查。
试想以下设​​置。

My aim is to make a missing dependency check between 2 given folders. Imagine the following setup.

Root\DirA\A.dll

Root\DirA\A.dll

Root\\ \\DirB\B.dll

Root\DirB\B.dll

b依赖上的。

所以,有了这些文件夹,我想创建一个新的AppDomain,加载B.DLL和具有DIRA(A.DLL)自动解决,并在新的AppDomain中分离出的依赖关系。

So given these folders, I want to create a new AppDomain, load B.dll and have the dependency from DirA(A.dll) automatically resolved and isolated in that new AppDomain.

隔离在这里是关键鉴于当我卸载该应用程序域我不想再创建一个新的具有潜在DIRA作为依赖,但需要它,以便在DIRC对DIRB依赖的情况下DIRC库,以及我希望它抛出一个异常。

Isolation is key here given that when I unload this AppDomain I want to create a new one with potentially DirA as a dependency again but DirC libraries that require it so in the case that DirC has a dependency on DirB as well I want it to throw an exception.

编辑:添加的情况下,它可以帮助说明我的问题更好的代码示例

Adding a code example in case that it helps describe my question better.

AppDomainSetup setup = new AppDomainSetup();
setup.ApplicationBase = @"C:\Root";
setup.ApplicationName = "Isolated Domain"
setup.PrivateBinPath = @"DirA;DirB";
setup.PrivateBinPathProbe = "";//disable search in AppBase..
var domain = AppDomain.CreateDomain(Guid.NewGuid().ToString(),
                                    AppDomain.CurrentDomain.Evidence,
                                    setup,
                                    AppDomain.CurrentDomain.PermissionSet);
//The following statement in theory should pick B.dll's dependency from DirA.
var assembly = domain.Load(AssemblyName.GetAssemblyName(@"C:\Root\DirB\B.dll").Name);
//Do the same in a different domain for C.dll



谢谢这一点。

Thanks for any help on that.

推荐答案

这看起来像是对的 ResolveEventHandler (更多的 MSDN关于解决未知组件

This looks like a job for the ResolveEventHandler (more detail on MSDN regarding resolving unknown assemblies)

所以,你可以写的东西像

So, you can write something like

class MyResolver
{
  public static Assembly MyResolveEventHandler( Object sender, ResolveEventArgs args )
  {
    // confirm args.Name contains A.dll
    String dllName = args.Name.Split({','}, SplitStringOptions.None)[0];
    if (dllName == "A")
    {
      return Assembly.LoadFile(@"C:\Root\DirA\A.dll")
    }
    return null;
  }
}

和你创建的域,你会怎么做答:

and in the domain you created, you'd do a:

domain.AssemblyResolve += new ResolveEventHandler(MyResolver.MyResolveEventHandler);

请确保您在B中引用一个以前绑定的事件。

Make sure you bind the event before you reference A in B.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆