By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,136 Members | 1,205 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,136 IT Pros & Developers. It's quick & easy.

Assembly.Load unload separate AppDomain problem

P: n/a
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.E rror);

}
File.Copy(newDLL,oldDLL,true);

File.Delete(newDLL);
Nov 16 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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/Ap...#_Toc514058498
"Lauren Hines" <du*****@spambeware.com> wrote in message
news:OM**************@TK2MSFTNGP12.phx.gbl...
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.E rror);

}
File.Copy(newDLL,oldDLL,true);

File.Delete(newDLL);

Nov 16 '05 #2

P: n/a
Thank you for your help.
I got it up and running last week.

Lauren

"David Levine" <no****************@wi.rr.com> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
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/Ap...#_Toc514058498
"Lauren Hines" <du*****@spambeware.com> wrote in message
news:OM**************@TK2MSFTNGP12.phx.gbl...
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.E rror);

}
File.Copy(newDLL,oldDLL,true);

File.Delete(newDLL);


Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.