[英]Loading assembly with dependencies in AppDomain trouble
嘗試在新的AppDomain中加載程序集時遇到一些問題。 我的“工作”程序集未加載,而是從引用加載了程序集。 我的代碼有什么問題?
using Autofac;
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Security;
using System.Security.Permissions;
using System.Security.Policy;
using System.Timers;
using Topshelf;
using Topshelf.Autofac;
using Vero.TaskScheduler.Core.Contracts;
namespace Vero.TaskScheduler.Host {
class Program {
static void Main(string[] args) {
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
AppDomainSetup domaininfo = new AppDomainSetup();
domaininfo.ApplicationBase = System.Environment.CurrentDirectory;
Evidence adevidence = AppDomain.CurrentDomain.Evidence;
AppDomain domain = AppDomain.CreateDomain("MyDomain", adevidence, domaininfo);
domain.Load(System.IO.File.ReadAllBytes("f:\\Autofac.dll"));
domain.Load(System.IO.File.ReadAllBytes("f:\\linq2db.dll"));
domain.Load(System.IO.File.ReadAllBytes("f:\\Quartz.dll"));
domain.Load(System.IO.File.ReadAllBytes("f:\\Vero.TaskScheduler.Core.dll"));
domain.Load(System.IO.File.ReadAllBytes("f:\\Vero.TaskScheduler.TestRefLib.dll"));
domain.Load(System.IO.File.ReadAllBytes("f:\\Vero.TaskScheduler.TestTask.dll"));
domain.Load(System.IO.File.ReadAllBytes("f:\\Vero.TaskScheduler.TestTask.dll"));
var i = domain.GetAssemblies();
return;
}
}
PS程序集“ Vero.TaskScheduler.TestTask.dll”在未引用“ Vero.TaskScheduler.TestRefLib.dll”時加載成功,否則未加載
我解決了我的問題。 添加AssemblyResolve事件句柄,如下所示:
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
和處理程序
private static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) {
return Assembly.LoadFile($"f:\\{args.Name.Split(',')[0]}.dll");
}
謝謝大家!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.