473,395 Members | 1,457 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,395 software developers and data experts.

AppDomain and Unload

Hi there. There are various snippets on forums regarding issues with
AppDomain.Unload and how it just doesnt work.

Fortunately, I got it working with the base case, after much fiddling.
Consider this 5 line program:

AppDomain domain = AppDomain.CreateDomain("MyDomain");
domain.CreateInstance("TempDLL", "TempDLL.Class1");
MessageBox.Show("try deleting file now"); //cant delete file
AppDomain.Unload(domain);
MessageBox.Show("try again"); //can delete file
Interstingly enough, if you use domain.Load( ) instead of createInstance you
can't delete the file as per the last line! (see
http://www.dotnet247.com/247referenc.../10/54067.aspx)

So getting around this was the first hurdle (also remember to use the name
of the dll WITHOUT the extension)

Now the task of putting it to work in the real application!

Unfortunately it didnt work - I got the the following errors when doing
AppDomain.Unload( )

An unhandled exception of type 'System.Threading.ThreadAbortException'
occurred in Unknown Module.
Additional information: Thread was being aborted.

(note these errors came out on console, they arent handle-able) - I'm not
sure that this would even prevent the module from unloading. So I'm not
convinced that this is the one error that is stopping the module being
unloaded. I have read that there are complications doing AppDomain.Unload( )
if you use API calls but i was thinking this shouldnt matter because I am
disposing of my main class and calling GC before unloading.

Any help

Wal
Nov 16 '05 #1
6 8155
Hi Wal:

When you say you are putting to work in the real application, are you
using Unwrap on the ObjectHandle object returned by CreateInstance? As
soon as this happens the assembly loads into your first appdomain and
is un-loadable.

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Tue, 16 Nov 2004 19:13:57 +1100, "Wal Turner" <vo****@hotmail.com>
wrote:
Hi there. There are various snippets on forums regarding issues with
AppDomain.Unload and how it just doesnt work.

Fortunately, I got it working with the base case, after much fiddling.
Consider this 5 line program:

AppDomain domain = AppDomain.CreateDomain("MyDomain");
domain.CreateInstance("TempDLL", "TempDLL.Class1");
MessageBox.Show("try deleting file now"); //cant delete file
AppDomain.Unload(domain);
MessageBox.Show("try again"); //can delete file
Interstingly enough, if you use domain.Load( ) instead of createInstance you
can't delete the file as per the last line! (see
http://www.dotnet247.com/247referenc.../10/54067.aspx)

So getting around this was the first hurdle (also remember to use the name
of the dll WITHOUT the extension)

Now the task of putting it to work in the real application!

Unfortunately it didnt work - I got the the following errors when doing
AppDomain.Unload( )

An unhandled exception of type 'System.Threading.ThreadAbortException'
occurred in Unknown Module.
Additional information: Thread was being aborted.

(note these errors came out on console, they arent handle-able) - I'm not
sure that this would even prevent the module from unloading. So I'm not
convinced that this is the one error that is stopping the module being
unloaded. I have read that there are complications doing AppDomain.Unload( )
if you use API calls but i was thinking this shouldnt matter because I am
disposing of my main class and calling GC before unloading.

Any help

Wal


Nov 16 '05 #2
Scott, thanks for your reply.

I am indeed using Unwrap( ) to get a ref to the main class. I'm baffled as
to why this would be loaded into the first appdomain. It would seem logical
to load into the domain i just created!

Do you know where I can find more about this or suggest a way that it can be
worked around?

Regards

Wal
"Scott Allen" <bitmask@[nospam].fred.net> wrote in message
news:hn********************************@4ax.com...
Hi Wal:

When you say you are putting to work in the real application, are you
using Unwrap on the ObjectHandle object returned by CreateInstance? As
soon as this happens the assembly loads into your first appdomain and
is un-loadable.

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Tue, 16 Nov 2004 19:13:57 +1100, "Wal Turner" <vo****@hotmail.com>
wrote:
Hi there. There are various snippets on forums regarding issues with
AppDomain.Unload and how it just doesnt work.

Fortunately, I got it working with the base case, after much fiddling.
Consider this 5 line program:

AppDomain domain = AppDomain.CreateDomain("MyDomain");
domain.CreateInstance("TempDLL", "TempDLL.Class1");
MessageBox.Show("try deleting file now"); //cant delete file
AppDomain.Unload(domain);
MessageBox.Show("try again"); //can delete file
Interstingly enough, if you use domain.Load( ) instead of createInstance
you
can't delete the file as per the last line! (see
http://www.dotnet247.com/247referenc.../10/54067.aspx)

So getting around this was the first hurdle (also remember to use the name
of the dll WITHOUT the extension)

Now the task of putting it to work in the real application!

Unfortunately it didnt work - I got the the following errors when doing
AppDomain.Unload( )

An unhandled exception of type 'System.Threading.ThreadAbortException'
occurred in Unknown Module.
Additional information: Thread was being aborted.

(note these errors came out on console, they arent handle-able) - I'm
not
sure that this would even prevent the module from unloading. So I'm not
convinced that this is the one error that is stopping the module being
unloaded. I have read that there are complications doing
AppDomain.Unload( )
if you use API calls but i was thinking this shouldnt matter because I am
disposing of my main class and calling GC before unloading.

Any help

Wal

Nov 16 '05 #3
The problem is that you are loading the assembly into the context of the
default appdomain, not the remote appdomain that you just created. You need
to create a class that will proxy the loading/unloading of assemblies for
you and evaluate those assemblies entirely in the remote appdomain, and then
unload that appdomain.

As you pass a reference of the loaded assembly back to the calling appdomain
the runtime will load another copy of that assembly into that appdomain -
this will lock the file and prevent it from being deleted.

This link explains most of what you will need to know and provides sample
code too.
http://www.gotdotnet.com/team/clr/AppdomainFAQ.aspx

Dave

"Wal Turner" <vo****@hotmail.com> wrote in message
news:Ob**************@TK2MSFTNGP12.phx.gbl...
Hi there. There are various snippets on forums regarding issues with
AppDomain.Unload and how it just doesnt work.

Fortunately, I got it working with the base case, after much fiddling.
Consider this 5 line program:

AppDomain domain = AppDomain.CreateDomain("MyDomain");
domain.CreateInstance("TempDLL", "TempDLL.Class1");
MessageBox.Show("try deleting file now"); //cant delete file
AppDomain.Unload(domain);
MessageBox.Show("try again"); //can delete file
Interstingly enough, if you use domain.Load( ) instead of createInstance
you can't delete the file as per the last line! (see
http://www.dotnet247.com/247referenc.../10/54067.aspx)

So getting around this was the first hurdle (also remember to use the name
of the dll WITHOUT the extension)

Now the task of putting it to work in the real application!

Unfortunately it didnt work - I got the the following errors when doing
AppDomain.Unload( )

An unhandled exception of type 'System.Threading.ThreadAbortException'
occurred in Unknown Module.
Additional information: Thread was being aborted.

(note these errors came out on console, they arent handle-able) - I'm
not sure that this would even prevent the module from unloading. So I'm
not convinced that this is the one error that is stopping the module being
unloaded. I have read that there are complications doing
AppDomain.Unload( ) if you use API calls but i was thinking this shouldnt
matter because I am disposing of my main class and calling GC before
unloading.

Any help

Wal

Nov 16 '05 #4
Hi Wal:

Well, when you Unwrap then the original app domain needs a definition
of the type in question - it needs all the same nitty gritty details
about the type's members and attributes that the second app domain
has.

There is some more info here:
http://www.gotdotnet.com/team/clr/Ap...#_Toc514058497

Do you just need to be able to delete the files? You might be able to
just shadow copy the assembly.

There are more details in this post and in the comments:
http://blogs.msdn.com/jasonz/archive...31/145105.aspx

and:
http://blogs.msdn.com/suzcook/archiv.../08/57211.aspx

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Wed, 17 Nov 2004 09:47:37 +1100, "Wal Turner" <vo****@hotmail.com>
wrote:
Scott, thanks for your reply.

I am indeed using Unwrap( ) to get a ref to the main class. I'm baffled as
to why this would be loaded into the first appdomain. It would seem logical
to load into the domain i just created!

Do you know where I can find more about this or suggest a way that it can be
worked around?

Regards

Wal
"Scott Allen" <bitmask@[nospam].fred.net> wrote in message
news:hn********************************@4ax.com.. .
Hi Wal:

When you say you are putting to work in the real application, are you
using Unwrap on the ObjectHandle object returned by CreateInstance? As
soon as this happens the assembly loads into your first appdomain and
is un-loadable.

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Tue, 16 Nov 2004 19:13:57 +1100, "Wal Turner" <vo****@hotmail.com>
wrote:
Hi there. There are various snippets on forums regarding issues with
AppDomain.Unload and how it just doesnt work.

Fortunately, I got it working with the base case, after much fiddling.
Consider this 5 line program:

AppDomain domain = AppDomain.CreateDomain("MyDomain");
domain.CreateInstance("TempDLL", "TempDLL.Class1");
MessageBox.Show("try deleting file now"); //cant delete file
AppDomain.Unload(domain);
MessageBox.Show("try again"); //can delete file
Interstingly enough, if you use domain.Load( ) instead of createInstance
you
can't delete the file as per the last line! (see
http://www.dotnet247.com/247referenc.../10/54067.aspx)

So getting around this was the first hurdle (also remember to use the name
of the dll WITHOUT the extension)

Now the task of putting it to work in the real application!

Unfortunately it didnt work - I got the the following errors when doing
AppDomain.Unload( )

An unhandled exception of type 'System.Threading.ThreadAbortException'
occurred in Unknown Module.
Additional information: Thread was being aborted.

(note these errors came out on console, they arent handle-able) - I'm
not
sure that this would even prevent the module from unloading. So I'm not
convinced that this is the one error that is stopping the module being
unloaded. I have read that there are complications doing
AppDomain.Unload( )
if you use API calls but i was thinking this shouldnt matter because I am
disposing of my main class and calling GC before unloading.

Any help

Wal


Nov 16 '05 #5
Dave thank you for your reply and link. Just in case you were wondering,
the underlying problem here is loading a DLL, then doing an update
(downloads to tmp file) then the download needs to overwrite the DLL.

Would you mind explainging what you mean by 'loading assembly into the
context of the default appdomain' ?
I am wondering at which line this would be occuring in the code. If i were
to create a class whose sole job was loading/unloading assemblies, i'm
unsure how it would be called from the newly created domain and not the
default domain. Basically I don't understand this unless what is meant is
that we replace

domain.CreateInstance("TempDLL", "TempDLL.Class1");

with

domain.CreateInstance("TempDLL", "TempDLL.ProxyClassThatLoadsClass1");
?
"David Levine" <no****************@wi.rr.com> wrote in message
news:OY**************@TK2MSFTNGP15.phx.gbl...
The problem is that you are loading the assembly into the context of the
default appdomain, not the remote appdomain that you just created. You
need to create a class that will proxy the loading/unloading of assemblies
for you and evaluate those assemblies entirely in the remote appdomain,
and then unload that appdomain.

As you pass a reference of the loaded assembly back to the calling
appdomain the runtime will load another copy of that assembly into that
appdomain - this will lock the file and prevent it from being deleted.

This link explains most of what you will need to know and provides sample
code too.
http://www.gotdotnet.com/team/clr/AppdomainFAQ.aspx

Dave

"Wal Turner" <vo****@hotmail.com> wrote in message
news:Ob**************@TK2MSFTNGP12.phx.gbl...
Hi there. There are various snippets on forums regarding issues with
AppDomain.Unload and how it just doesnt work.

Fortunately, I got it working with the base case, after much fiddling.
Consider this 5 line program:

AppDomain domain = AppDomain.CreateDomain("MyDomain");
domain.CreateInstance("TempDLL", "TempDLL.Class1");
MessageBox.Show("try deleting file now"); //cant delete file
AppDomain.Unload(domain);
MessageBox.Show("try again"); //can delete file
Interstingly enough, if you use domain.Load( ) instead of createInstance
you can't delete the file as per the last line! (see
http://www.dotnet247.com/247referenc.../10/54067.aspx)

So getting around this was the first hurdle (also remember to use the
name of the dll WITHOUT the extension)

Now the task of putting it to work in the real application!

Unfortunately it didnt work - I got the the following errors when doing
AppDomain.Unload( )

An unhandled exception of type 'System.Threading.ThreadAbortException'
occurred in Unknown Module.
Additional information: Thread was being aborted.

(note these errors came out on console, they arent handle-able) - I'm
not sure that this would even prevent the module from unloading. So I'm
not convinced that this is the one error that is stopping the module
being unloaded. I have read that there are complications doing
AppDomain.Unload( ) if you use API calls but i was thinking this shouldnt
matter because I am disposing of my main class and calling GC before
unloading.

Any help

Wal


Nov 16 '05 #6
inline

"Wal Turner" <vo****@hotmail.com> wrote in message
news:Ok**************@TK2MSFTNGP12.phx.gbl...
Dave thank you for your reply and link. Just in case you were wondering,
the underlying problem here is loading a DLL, then doing an update
(downloads to tmp file) then the download needs to overwrite the DLL.

Would you mind explainging what you mean by 'loading assembly into the
context of the default appdomain' ?
Basically, whenever you do this...

Assembly a = remoteDomain.Load(xxx);

it loads the assembly into both appdomains - the remote one directly when
the .Load executes, and the calling appdomain when a reference to the loaded
assembly is returned to the calling appdomain.

Whenever you reference an object as a concrete type the runtime has to load
the metadata for that type into the current appdomain, so even if the object
instance is remoted the metadata for that object is still loaded into the
appdomain.

I am wondering at which line this would be occuring in the code. If i were
to create a class whose sole job was loading/unloading assemblies, i'm
unsure how it would be called from the newly created domain and not the
default domain. Basically I don't understand this unless what is meant is
that we replace

domain.CreateInstance("TempDLL", "TempDLL.Class1");

with

domain.CreateInstance("TempDLL", "TempDLL.ProxyClassThatLoadsClass1");
?


When you create an instance and then access the object it has to load the
metadata. which causes the assembly to get loaded into both appdomains.

One way you can handle this is something like this...

void DoSomeWorkFromCallingAppdomain()
{ // this is the routine you want to do the work.
AppDomain remoteDomain = AppDomain.CreateDomain(...);
RemoteHelper helper =
(RemoteHelper)remoteDomain.CreateInstanceFromAndUn wrap(...);
helper.DoSomeWork();
AppDomain.Unload(remoteDomain);
}

class RemoteHelper : MarshalByRefObj
{
public string DoSomeWork()
{
// create other objects in other assemblies, do work, and return .
Assembly a = Assembly.Load("AsmToBeUnloadedLater");
return "SomeKindOfResult";
}
}

The class RemoteHelper can be in the same assembly as the method
DoSomeWorkFromCallingAppdomain. This routine accesses the types and
assemblies that you want to unload later on. Basically you have to isolate
all the types and only access the ones you want to unload from within
methods in class RemoteHelper. The data returned from the remoted method
must not be defined in the assemblies you want to unload.

Another possibility (one I have not tried yet) is to use Shadow copying to
allow you to overwrite the assembly without needing to unload the assembly.
You wont be able to load the new assembly into the appdomain until you
recycle the appdomain (which may be a problem for you default appdomain) but
you can at least overwrite the bits.
Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Satinderpal Singh | last post by:
Hi All, I have an EXE, I load the DLL from that exe in a seperate AppDomain. (I have not given reference to that DLL from the EXE). Now, i call some commands of that dll from the EXE, and in...
4
by: stu_pb | last post by:
I am designing a plugin system for a window application using .NET(C# specifically). One of the requirements of the plugin system is to be able to dynamically load/unload plugins. My initial...
4
by: Chris Lacey | last post by:
Hi, I'm currently writing a scheduling service which starts a number DotNet executables, each within a new AppDomain, every ten seconds. The guts of the code is as follows: // For each...
4
by: Mirano | last post by:
Hi everybody. I load an assembly into another AppDomain, not a default one. As there is no way to unload the assembly, I need to unload the domain. This is where the app hangs. The problem is...
2
by: Lauren Hines | last post by:
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....
22
by: JPSutor | last post by:
when I use the AppDomain.UnLoad method (which calls the Thread.Abort method), I get the following error message AppDomain can not be unloaded because the thread 1378 can not be unwound out of it...
3
by: Tao | last post by:
hi.. group, A wired question about FileSystemWatcher and Unload an AppDomain. I have a class A which creates class B. When B is created, B loads an AppDomain and execute some functions on the...
2
by: Tim | last post by:
Hello, I've finally managed to remotely load a DLL. I've expanded the code to load it in a seperate domain to unload the appdomain, which works to a certain extend. The host application always...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.