PC Review


Reply
Thread Tools Rate Thread

C# DLL unloading appdomain

 
 
Tim
Guest
Posts: n/a
 
      12th Jul 2007
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 keeps the entry DLL in memory. How can I
also unload this main DLL? As it's left after unloading the appdomain.
The dll is loaded in the hostapplication at "Assembly assembly =
appdom.Load(RawAssembly)".

How can I avoid this behaviour? I would like to invoke a method and
unload the everything dynamically loaded.

Thanks in advance!

The code fragment:

byte[] RawAssembly = getFileArray(FileName);
// AppDomain.CurrentDomain loads also an instance
Assembly assembly = appdom.Load(RawAssembly);

//Assembly assembly = Assembly.LoadFile(FileName);
foreach (AssemblyName ASSN in
assembly.GetReferencedAssemblies())
{
try
{
appdom.CreateInstance(ASSN.Name,
ASSN.GetType().Name);
//
Activator.CreateInstance(ASSN.GetType());
}
catch (Exception e)
{
// MessageBox.Show(e.Message);
}
}
foreach (Type type in assembly.GetTypes())
{
if (!type.IsClass || type.IsNotPublic)
continue;
Type[] interfaces = type.GetInterfaces();
//object obj = Activator.CreateInstance(type);
System.Runtime.Remoting.ObjectHandle t =
appdom.CreateInstance(assembly.GetName().ToString(), type.FullName);
tList.Add(t);
}

 
Reply With Quote
 
 
 
 
=?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
Guest
Posts: n/a
 
      12th Jul 2007
Tim,
You can't. You can create a new AppDomain, load an assembly into it, and
call methods in the remotely loaded assembly, and tear down the secondary
Appdomain, but the only way you can unload an assembly from your primary
appDomain is the tear down the primary appDomain itself.
Peter
--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder(BETA): http://www.blogmetafinder.com



"Tim" wrote:

> 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 keeps the entry DLL in memory. How can I
> also unload this main DLL? As it's left after unloading the appdomain.
> The dll is loaded in the hostapplication at "Assembly assembly =
> appdom.Load(RawAssembly)".
>
> How can I avoid this behaviour? I would like to invoke a method and
> unload the everything dynamically loaded.
>
> Thanks in advance!
>
> The code fragment:
>
> byte[] RawAssembly = getFileArray(FileName);
> // AppDomain.CurrentDomain loads also an instance
> Assembly assembly = appdom.Load(RawAssembly);
>
> //Assembly assembly = Assembly.LoadFile(FileName);
> foreach (AssemblyName ASSN in
> assembly.GetReferencedAssemblies())
> {
> try
> {
> appdom.CreateInstance(ASSN.Name,
> ASSN.GetType().Name);
> //
> Activator.CreateInstance(ASSN.GetType());
> }
> catch (Exception e)
> {
> // MessageBox.Show(e.Message);
> }
> }
> foreach (Type type in assembly.GetTypes())
> {
> if (!type.IsClass || type.IsNotPublic)
> continue;
> Type[] interfaces = type.GetInterfaces();
> //object obj = Activator.CreateInstance(type);
> System.Runtime.Remoting.ObjectHandle t =
> appdom.CreateInstance(assembly.GetName().ToString(), type.FullName);
> tList.Add(t);
> }
>
>

 
Reply With Quote
 
Tim
Guest
Posts: n/a
 
      13th Jul 2007
On 12 jul, 18:22, Peter Bromberg [C# MVP]
<pbromb...@yahoo.yabbadabbadoo.com> wrote:
> Tim,
> You can't. You can create a new AppDomain, load an assembly into it, and
> call methods in the remotely loaded assembly, and tear down the secondary
> Appdomain, but the only way you can unload an assembly from your primary
> appDomain is the tear down the primary appDomain itself.
> Peter
> --
> Site: http://www.eggheadcafe.com
> UnBlog: http://petesbloggerama.blogspot.com
> BlogMetaFinder(BETA): http://www.blogmetafinder.com
>
> "Tim" wrote:
> > 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 keeps the entry DLL in memory. How can I
> > also unload this main DLL? As it's left after unloading the appdomain.
> > The dll is loaded in the hostapplication at "Assembly assembly =
> > appdom.Load(RawAssembly)".

>
> > How can I avoid this behaviour? I would like to invoke a method and
> > unload the everything dynamically loaded.

>
> > Thanks in advance!

>
> > The code fragment:

>
> > byte[] RawAssembly = getFileArray(FileName);
> > // AppDomain.CurrentDomain loads also an instance
> > Assembly assembly = appdom.Load(RawAssembly);

>
> > //Assembly assembly = Assembly.LoadFile(FileName);
> > foreach (AssemblyName ASSN in
> > assembly.GetReferencedAssemblies())
> > {
> > try
> > {
> > appdom.CreateInstance(ASSN.Name,
> > ASSN.GetType().Name);
> > //
> > Activator.CreateInstance(ASSN.GetType());
> > }
> > catch (Exception e)
> > {
> > // MessageBox.Show(e.Message);
> > }
> > }
> > foreach (Type type in assembly.GetTypes())
> > {
> > if (!type.IsClass || type.IsNotPublic)
> > continue;
> > Type[] interfaces = type.GetInterfaces();
> > //object obj = Activator.CreateInstance(type);
> > System.Runtime.Remoting.ObjectHandle t =
> > appdom.CreateInstance(assembly.GetName().ToString(), type.FullName);
> > tList.Add(t);
> > }


Peter, Thanks for you quick reply!

Is there a way to invoke methods on the dynamic assembly without
loading it in the main appdomain?

 
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
Unloading AppDomain won't release DLL handles MRe Microsoft C# .NET 2 18th Apr 2010 12:12 AM
Re: Unloading appdomain and freeing memory Champika Nirosh Microsoft C# .NET 0 7th Dec 2006 11:56 AM
AppDomain unloading problem =?Utf-8?B?RXRpZW5uZSBGb3J0aW4=?= Microsoft Dot NET Framework 0 1st Sep 2005 05:37 PM
CallContext and unloading AppDomain Michael Per Microsoft Dot NET Framework 0 2nd Aug 2005 06:18 PM
Unloading an AppDomain Phil Jones Microsoft Dot NET Framework 10 16th Apr 2005 10:10 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:31 AM.