我正在尝试将程序集(dll)加载到 AppDomain 并调用入口点。(基本上将包引导到 Azure 环境中)我一直在关注这篇 SO 文章(如何创建应用程序域并在其中运行我的应用程序?),我认为我做得对,但我遇到了一些问题。
我已经在这里使用了几篇文章来了解我的情况,但是我一直遇到FileNotFoundException
如Unable to load execution assembly into new AppDomain, FileNotFoundException中所述的问题。我的问题是该解决方案不起作用。我尝试执行的程序集存在于不同的位置。所以ApplicationBase
需要是我要执行的程序集的文件夹。
var otherType = typeof(BootstrapProxy);
var domaininfo = new AppDomainSetup
{
ConfigurationFile = executingAssembly + ".config",
ApplicationBase = _root
};
Evidence adevidence = AppDomain.CurrentDomain.Evidence;
_domain = AppDomain.CreateDomain(_type.ToString(), adevidence, domaininfo);
_domain.AssemblyResolve += (sender, args) =>
{
var lookupPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
if (lookupPath == null) return null;
var assemblyname = new AssemblyName(args.Name).Name;
var assemblyFileName = Path.Combine(lookupPath, assemblyname + ".dll");
var assembly = Assembly.LoadFrom(assemblyFileName);
return assembly;
};
var proxy = _domain.CreateInstanceAndUnwrap(otherType.Assembly.FullName, otherType.FullName) as BootstrapProxy;
最后一行抛出异常,并且 AssemblyResolve 事件永远不会触发(通过在该var lookupPath
行上放置断点来确定。
我也AppDomain.CurrentDomain.AssemblyResolve
用与上面相同的处理程序尝试了该事件,但没有运气。我也尝试在BootstrapProxy
类中创建相同的处理程序。
我认为我这样做是正确的,但请记下第一段,所以如果我完全偏离基础,我不反对以不同的方式做事。
更新:
我已经更改了代码以强制将程序集加载到新的 appdomain 中,但仍然存在问题。
var otherType = typeof(BootstrapProxy);
var domaininfo = new AppDomainSetup
{
ConfigurationFile = executingAssembly + ".config",
ApplicationBase = _root
};
Evidence adevidence = AppDomain.CurrentDomain.Evidence;
_domain = AppDomain.CreateDomain(_type.ToString(), adevidence, domaininfo);
var deps = Assembly.GetExecutingAssembly().GetReferencedAssemblies();
foreach (var dep in deps)
{
var lookupPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
if (lookupPath == null) continue;
var assemblyname = new AssemblyName(dep.Name).Name;
var assemblyFileName = Path.Combine(lookupPath, assemblyname + ".dll");
if (File.Exists(assemblyFileName))
_domain.Load(File.ReadAllBytes(assemblyFileName));
}
_domain.Load(File.ReadAllBytes(Assembly.GetExecutingAssembly().Location));
var sl = _domain.GetAssemblies().ToArray();
var proxy = _domain.CreateInstanceAndUnwrap(otherType.Assembly.FullName, otherType.FullName) as BootstrapProxy;
sl
显示所有 dll,包括在FileNotFoundException
新的 appdomain 中引用的那个。
public class BootstrapProxy : MarshalByRefObject
{
public void Main()
{
Console.WriteLine("Magic happened.");
}
}
更新 2:
我把它改成这样:
var deps = Assembly.GetExecutingAssembly().GetReferencedAssemblies();
foreach (var dep in deps)
{
var lookupPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
if (lookupPath == null) continue;
var assemblyname = new AssemblyName(dep.Name).Name;
var assemblyFileName = Path.Combine(lookupPath, assemblyname + ".dll");
if (File.Exists(assemblyFileName))
File.Copy(assemblyFileName, Path.Combine(_root, assemblyname + ".dll"));
}
File.Copy(Assembly.GetExecutingAssembly().Location, Path.Combine(_root, Path.GetFileName(Assembly.GetExecutingAssembly().Location)));
var otherType = typeof(BootstrapProxy);
var domaininfo = new AppDomainSetup
{
ConfigurationFile = executingAssembly + ".config",
ApplicationBase = _root
};
Evidence adevidence = AppDomain.CurrentDomain.Evidence;
_domain = AppDomain.CreateDomain(_type.ToString(), adevidence, domaininfo);
var proxy = _domain.CreateInstanceAndUnwrap(otherType.Assembly.FullName, otherType.FullName) as BootstrapProxy;
if (proxy != null)
{
proxy.Main();
}
这种复制程序集及其对新 AppDomain 的 ApplicationBase 的引用的方法并不理想,因为有一些常见的引用,我最终可能会遇到版本冲突和其他问题。