简体   繁体   English

在AppDomain中加载具有依赖项的程序集的麻烦

[英]Loading assembly with dependencies in AppDomain trouble

I have some problem when trying to load assembly in new AppDomain. 尝试在新的AppDomain中加载程序集时遇到一些问题。 My "working" assembly is not loading, but instead it, loads assembly from references. 我的“工作”程序集未加载,而是从引用加载了程序集。 What is wrong in my code? 我的代码有什么问题?

在此处输入图片说明

    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 Assembly "Vero.TaskScheduler.TestTask.dll" load succesful when it not refered to "Vero.TaskScheduler.TestRefLib.dll", else it's not loaded PS程序集“ Vero.TaskScheduler.TestTask.dll”在未引用“ Vero.TaskScheduler.TestRefLib.dll”时加载成功,否则未加载

I resolved my problem. 我解决了我的问题。 Add AssemblyResolve event handle like this: 添加AssemblyResolve事件句柄,如下所示:

            AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;

And handler 和处理程序

    private static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) {
         return Assembly.LoadFile($"f:\\{args.Name.Split(',')[0]}.dll");
    }

Thanks all!!! 谢谢大家!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM