473,385 Members | 1,528 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Assembly.Load unload separate AppDomain problem

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
2 10796
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mike Krueger | last post by:
Hi I'm currently working on a forms designer for a free .NET IDE (SharpDevelop -> www.icsharpcode.net/OpenSource/SD). problem: I try to put 'custom' components (user controls from the current...
1
by: Luis Pinho | last post by:
Hi There, I've got a server that is waiting for requests, these request correspond to calls to objects that are specified in assemblies stored in the GAC. To do this, I use reflection to call...
2
by: Sam Martin | last post by:
Morning all, Right, I've read untold articles now, listening to people bitch about there being no Unload method for Assembly. Plenty of people do counter that this is possible by loading the...
4
by: Igor | last post by:
I have an application which loads assemblies from assembly cache. Assembly cannot be unloaded from memory without unloading the entire AppDomain, so every time I compile assemblies with new...
2
by: Dominique Vandensteen | last post by:
I want to make a program that checks for updates at start... The way I am doing this is to create 2 projects... A simple exe checks the server for the version and downloads a dll if needed. The...
0
by: Fred | last post by:
Hello, I am using a separate app domain to load an assembly from either disk or cache, create an instance of it and run a method. I then call dispose on the created instance and unload the app...
3
by: Frank Uray | last post by:
Hi all I have a problem with loading a assembly ... I am trying to do the following: - I have a directory with a dll (assembly) in it (not the currect dir.) - I am trying to load this assembly,...
3
by: navyliu | last post by:
I have raised a discussion about assembly unloading.But we can't get a final solution. Since we cannot unlaod a assembly,Is there any reference about this indicate that this problem won't make...
1
by: Basti | last post by:
Hello, I'm working with .Net 2.0. I tried to get attributes of an unloaded assembly. So, the sole way I know is to create a new child application domain, in this app domain I load the assembly,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.