Load assemblies with dependencies in a different AppDomain

左心房为你撑大大i 提交于 2019-12-02 00:21:58

问题


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

Root\DirA\A.dll

Root\DirB\B.dll

B depends on A.

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.

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.

Edit: 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.


回答1:


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

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




回答2:


AppDomain's cannot probe for dll's outside of their initial folder. They can probe in the GAC, and in the PrivateBinPath deeper into the folder, but they cannot probe into other folders.



来源:https://stackoverflow.com/questions/11989896/load-assemblies-with-dependencies-in-a-different-appdomain

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!