装载组件在新的AppDomain没有加载在父母的AppDomain [英] Load Assembly in New AppDomain without loading it in Parent AppDomain

查看:177
本文介绍了装载组件在新的AppDomain没有加载在父母的AppDomain的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图加载一个dll到一个控制台应用程序,然后卸载它,并彻底删除该文件。我遇到的问题是,加载DLL在其自己的AppDomain的行为会在家长的AppDomain参考因此不能让我破坏的dll文件,除非我完全关闭程序。进行此code的工作有什么想法?

 字符串fileLocation = @C:\ Collector.dll;
AppDomain中域= AppDomain.CreateDomain(fileLocation);
domain.Load(@Services.Collector);
AppDomain.Unload(域);
 

顺便说一句,我也试过这种code,没有运气或者

 字符串fileLocation = @C:\ Collector.dll;
byte []的assemblyFileBuffer = File.ReadAllBytes(fileLocation);

AppDomainSetup domainSetup =新AppDomainSetup();
domainSetup.ApplicationBase = Environment.CurrentDirectory;
domainSetup.ShadowCopyFiles =真;
domainSetup.CachePath = Environment.CurrentDirectory;
AppDomain中tempAppDomain = AppDomain.CreateDomain(Services.Collector,AppDomain.CurrentDomain.Evidence,domainSetup);

//装载了临时组装和做的东西
大会projectAssembly = tempAppDomain.Load(assemblyFileBuffer);

//然后我试图清理
AppDomain.Unload(tempAppDomain);
tempAppDomain = NULL;
File.Delete(fileLocation);
 

解决方案

行,所以我在这里解决我自己的问题。显然,如果调用AppDomain.Load它会与你的父母的AppDomain注册。所以,简单地足够的答案是不要引用它。这是链接到一个网站,显示了如何正确设置它。

https://bookie.io/bmark/readable/9503538d6bab80

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); 

解决方案

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆