PC Review


Reply
Thread Tools Rate Thread

Assembly.Load unload separate AppDomain problem

 
 
Lauren Hines
Guest
Posts: n/a
 
      19th May 2004
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);


 
Reply With Quote
 
 
 
 
David Levine
Guest
Posts: n/a
 
      20th May 2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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);
>
>



 
Reply With Quote
 
Lauren Hines
Guest
Posts: n/a
 
      24th May 2004
Thank you for your help.
I got it up and running last week.

Lauren

"David Levine" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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);
> >
> >

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
load and unload assembly (System.AppDomain) =?Utf-8?B?RnJhbmsgVXJheQ==?= Microsoft C# .NET 3 30th Mar 2006 12:34 PM
load and unload assembly (System.AppDomain) =?Utf-8?B?RnJhbmsgVXJheQ==?= Microsoft C# .NET 0 26th Mar 2006 11:47 PM
New To AppDomains, want to load assembly, read attributes, then release reference (unload AppDomain) WALDO Microsoft Dot NET Framework 0 4th Nov 2005 06:49 PM
Load / Unload AppDomain & memory Perry Microsoft C# .NET 1 16th Apr 2005 01:09 PM
unload assembly - AppDomain.Unload does not work as expected =?Utf-8?B?RGllcmsgRHJvdGg=?= Microsoft Dot NET Framework 2 4th Apr 2004 04:06 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:13 PM.