在新 AppDomain 中加载程序集而不在父 AppDomain 中加载它 [英] Load Assembly in New AppDomain without loading it in Parent AppDomain
问题描述
我正在尝试将 dll 加载到控制台应用程序中,然后将其卸载并完全删除该文件.我遇到的问题是,在自己的 AppDomain 中加载 dll 的行为在父 AppDomain 中创建了一个引用,因此除非我完全关闭程序,否则不允许我销毁 dll 文件.有没有让这段代码工作的想法?
I am attempting to load a dll into a console app and then unload it and delete the file completely. The problem I am having is that the act of loading the dll in its own AppDomain creates a reference in the Parent AppDomain thus not allowing me to destroy the dll file unless I totally shut down the program. Any thoughts on making this code work?
string fileLocation = @"C:\Collector.dll";
AppDomain domain = AppDomain.CreateDomain(fileLocation);
domain.Load(@"Services.Collector");
AppDomain.Unload(domain);
顺便说一句,我也试过这个代码,但也没有运气
BTW I have also tried this code with no luck either
string fileLocation = @"C:\Collector.dll";
byte[] assemblyFileBuffer = File.ReadAllBytes(fileLocation);
AppDomainSetup domainSetup = new AppDomainSetup();
domainSetup.ApplicationBase = Environment.CurrentDirectory;
domainSetup.ShadowCopyFiles = "true";
domainSetup.CachePath = Environment.CurrentDirectory;
AppDomain tempAppDomain = AppDomain.CreateDomain("Services.Collector", AppDomain.CurrentDomain.Evidence, domainSetup);
//Load up the temp assembly and do stuff
Assembly projectAssembly = tempAppDomain.Load(assemblyFileBuffer);
//Then I'm trying to clean up
AppDomain.Unload(tempAppDomain);
tempAppDomain = null;
File.Delete(fileLocation);
推荐答案
好的,所以我在这里解决了我自己的问题.显然,如果您调用 AppDomain.Load,它将向您的父 AppDomain 注册它.因此,答案很简单,根本不引用它.这是显示如何正确设置的站点的链接.
OK so I solved my own issue here. Apparently if you call AppDomain.Load it will register it with your parent AppDomain. So simply enough the answer is not to reference it at all. This is the link to a site that shows how to set this up properly.
https://bookie.io/bmark/readable/9503538d6bab80
这篇关于在新 AppDomain 中加载程序集而不在父 AppDomain 中加载它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!