views:

346

answers:

1

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); 
+2  A: 

Ok so I solved my own issue here. Aparently 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 peroperly.

http://www.devsource.com/c/a/Using-VS/Dynamic-Plugins-Using-the-codeAppDomaincode-Class-to-Load-and-Unload-Code/1/

Al Katawazi
Congratulationsand jubilationsI want the world to know I'm happy as can be.
Seventh Element