Unloading AppDomain won't release DLL handles

M

MRe

Hi,

I'm trying to use an AppDomain to load an Assembly DLL that I can
later unload (and release the lock on the DLL). I can't seem to find
any examples that are easy to follow (none of them just stick to the
issue, they all seem to include a ton of caching and dynamic-code,
etc. fluff with them). I just want the bare minimum to load and unload
a managed DLL.

Below is what I've got so far. It's what I've so far gathered to be
that bare minimum I'm looking for.. however, it doesn't work. When the
AppDomain is unloaded, the handle to my Library.dll remains open - I
can see this by running www.sysinternals.com's handle.exe after the
AppDomain is unloaded.

Can someone please explain what I'm doing wrong?

Thank you,
Kind regards,
Eliott

// Project: Program; File: Program.cs; Output: Program.exe
using System;
using System.Reflection;
using System.IO;
using System.Text;
namespace Problem
{ class Program
{ static void Main(string[] args)
{

AppDomainSetup ads = new AppDomainSetup();
// Seen this somewhere as a possible solution
// to my problem, but it doesn't help
//ads.LoaderOptimization =
LoaderOptimization.MultiDomainHost;

AppDomain ad = AppDomain.CreateDomain("", null, ads);

// I'm expecting this to load the library only into the
// AppDomain I just created
Assembly a = ad.Load(File.ReadAllBytes("Library.dll"));

// Is this the correct way to access the method in the
// library, i.e., it's not going to cause the Assembly to
// be loaded into the wrong AppDomain?
/*
IPlugin Plugin = (IPlugin)ad.CreateInstanceAndUnwrap
( a.FullName
, "Problem.Library"
);
Console.WriteLine(Plugin.Go("hi"));
//*/

// I'm expecting this to unload the library, which
// I believe to only be locked by this AppDomain
AppDomain.Unload(ad);

// Running "handle.exe Library.dll" while the program is
// blocked here shows that the library is still open by the
// program - I'd expect it to be closed after having
// unloaded the AppDomain
Console.ReadKey();

}
}
}


// Project: Library; File: Library.cs; Output: Library.dll
using System;
namespace Problem
{ public class Library : MarshalByRefObject, IPlugin
{ public string Go(string Input)
{ return "Your input was [" + Input + "]";
}
}
}

// Project: IPlugin; File: IPlugin.cs; Output: IPlugin.dll
namespace Problem
{ public interface IPlugin
{ string Go(string Input);
}
}

// Keywords: AppDomain csharp DLL dotnet handle library load lock
managed plug-in plugin unload
 
T

Tom Shelton

Hi,

I'm trying to use an AppDomain to load an Assembly DLL that I can
later unload (and release the lock on the DLL). I can't seem to find
any examples that are easy to follow (none of them just stick to the
issue, they all seem to include a ton of caching and dynamic-code,
etc. fluff with them). I just want the bare minimum to load and unload
a managed DLL.

Because it's not any easy task to accomplish. It takes a fair amount of
infrastructure and planning to be able to accomplish dynamic load/unload.
Dynamic load is easy, unloading - not so much.

What it boils down to is that you have to create a proxy and use remoting to
isolate your dynamically loaded types in the new appdomain. If you so much as
refence one of the loaded types in your main appdomain, then the dll will be
loaded into it and you won't be able to unload it.

I have some example code here I would be willing to dig up, though it's
in C#, but you might be better off investing your time learning the System.AddIn
namespace introduced in 3.5 (hoping your using VB2008 targeting .net 3.5).
It provides the infrastructure for this already, along with versioning, etc.
Though, the learning curve seems to be a bit steep, it would proably be worth
it rather then writting all of the dynamic load/unload plumbing your self.
 
T

Tom Shelton

I have some example code here I would be willing to dig up, though it's
in C#

LOL... I thought I was in the VB.NET group for a minute - even after seeing
the OP's C# code :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top