473,403 Members | 2,222 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,403 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 10802
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,...
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: 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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.