Assembly.Load unload separate AppDomain problem

L

Lauren Hines

Hello,
I have read numerous post stating that the only way to unload an assembly
(DLL in my case) is to create a separate AppDomain, load the assembly, then
unload it by calling AppDomain.Unload.

When trying to delete the DLL file I get an exception that access is denied.
When trying to copy over the DLL file, I get an exception that it is being
used by another process.


Can anyone tell me what is wrong with the following code?
Much obliged.
Lauren

// must create a domain for unloading

System.Security.Policy.Evidence baseEvidence =
AppDomain.CurrentDomain.Evidence;

System.Security.Policy.Evidence adevidence = new
System.Security.Policy.Evidence(baseEvidence);

AppDomain ad = AppDomain.CreateDomain("Test",adevidence);


Assembly SampleAssembly;



// FileInfoManager is the name of my dll

// I have tried both of the following lines

//SampleAssembly = Assembly.Load("FileInfoManager,Version=1.0.0.0");

SampleAssembly = ad.Load("FileInfoManager,Version=1.0.0.0");




//unload the dll

AppDomain.Unload(ad);

ad = null;

SampleAssembly = null;

string oldDLL = System.Windows.Forms.Application.StartupPath +
"\\FileInfoManager.dll";

string newDLL = System.Windows.Forms.Application.StartupPath +
"\\FileInfoManager2.dll";

try

{

File.Delete(oldDLL);

}

catch(Exception exc)

{

log.WriteEntry("Exception deleting file " + oldDLL + " : " +
exc.Message,System.Diagnostics.EventLogEntryType.Error);

}


File.Copy(newDLL,oldDLL,true);

File.Delete(newDLL);
 
D

David Levine

Even though you are loading the assembly in the context of the new appdomain
you are returning a reference to it in the default appdomain, which causes
it to load the assembly in both appdomains. You need to write a class that
is remoted back to the default appdomain, and provide a method, e.g.
LoadAssembly() that does the actual loading.

This link explains many of the concepts and has some sample code that ought
to get you started.

http://www.gotdotnet.com/team/clr/AppdomainFAQ.aspx#_Toc514058498
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top