简体   繁体   中英

Loading assembly with dependencies in AppDomain trouble

I have some problem when trying to load assembly in new 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

I resolved my problem. Add AssemblyResolve event handle like this:

            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!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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