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
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