【发布时间】:2013-10-10 10:56:44
【问题描述】:
所以最近我一直在从事一个项目,其中应用程序(或可执行文件,无论你想怎么称呼它)需要能够加载和卸载在可执行文件的文件夹中找不到的程序集全部。 (甚至可能是另一个驱动器)
举个例子,我希望能够将我的应用程序放在 D:\AAA\theAppFolder ,并将 DLL 文件的程序集放在 C:\BBB\组件
彻底看,我发现 AppDomain 允许卸载自己和任何附加的程序集的能力,所以我想我会试一试,但是几个小时后似乎出现了问题值得尝试:AppDomains 无法查看应用程序库之外的任何地方。
根据 AppDomain 的纪录片(和我自己的经验),您不能在 ApplicationBase 之外设置 PrivateBinPath ,如果我将 ApplicationBase 设置在应用程序所在的驱动器之外(通过 AppDomainSetup),我会得到 System.IO。 FileNotFoundException 抱怨找不到应用程序本身。
因此,我什至无法达到可以使用 AssemblyResolve ResolveEventHandler 尝试使用 MarhsalByRefObject 继承类来获取程序集的阶段...
这里有一些与我目前正在尝试的代码相关的 sn-ps 代码
internal class RemoteDomain : MarshalByRefObject
{
public override object InitializeLifetimeService() //there's apparently an error for marshalbyref objects where they get removed after a while without this
{
return null;
}
public Assembly GetAssembly(byte[] assembly)
{
try
{
return Assembly.Load(assembly);
}
catch (Exception e)
{
Console.WriteLine(e);
}
return null;
}
public Assembly GetAssembly(string filepath)
{
try
{
return Assembly.LoadFrom(filepath);
}
catch (Exception e)
{
Console.WriteLine(e);
}
return null;
}
}
public static Assembly LoadAssembly(string modName, BinBuffer bb)
{
string assembly = pathDirTemp+"/"+modName+".dll";
File.WriteAllBytes(assembly, bb.ReadBytes(bb.BytesLeft()));
RemoteDomain loader = (RemoteDomain)modsDomain.CreateInstanceAndUnwrap(typeof(RemoteDomain).Assembly.FullName, typeof(RemoteDomain).FullName);
return loader.GetAssembly(assembly);
}
尽可能具体:有什么方法可以让可卸载的 AppDomain 加载不在应用程序基础文件夹中的程序集?
【问题讨论】:
-
如何创建
AppDomain? -
我有多种方法,目前的方法是 modsDomain = AppDomain.CreateDomain("randomname",null,new AppDomainSetup() { PrivateBinPath = AssembliesFolderPath });我知道路径是有效的,因为 Directory.CreateDirectory(在代码的另一部分调用)创建文件夹就好了。
-
使用 Assembly.Load(byte[]) 是自找麻烦。通过将程序集存储在临时目录中并将类型解包到主应用程序域中来复合。
标签: c# .net-assembly appdomain marshalbyrefobject