【问题标题】:Loading assembly with dependencies in AppDomain trouble在 AppDomain 问题中加载具有依赖项的程序集
【发布时间】:2018-08-01 07:00:22
【问题描述】:

尝试在新的 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;
       }
}

附:程序集“Vero.TaskScheduler.TestTask.dll”在未引用“Vero.TaskScheduler.TestRefLib.dll”时加载成功,否则未加载

【问题讨论】:

  • “程序集”标签与汇编语言 (asm) 相关,而不是 .Net 程序集。
  • 对不起,我是 StackOverflow 网站的新手 :-)
  • 我们都在同一时间。我只是提一下,以便您可以编辑您的问题以将其发送给合适的人。
  • 谢谢,我改了标签集
  • 请将您的示例代码直接添加到帖子中,而不是带有图像,图像可以被视为附加信息,但将代码作为文本包含在帖子中会更好

标签: c# .net-assembly appdomain


【解决方案1】:

我解决了我的问题。像这样添加 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");
    }

谢谢大家!!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 2010-09-06
    • 1970-01-01
    • 1970-01-01
    • 2014-04-25
    相关资源
    最近更新 更多