Problem unloading a DLL from a separate AppDomain.

C

Colin Svingen

Hello. I'm trying to reload a shadow copied DLL after it changes. I'm
loading it in a separate AppDomain, but it also gets loaded in my main
AppDomain, so when I unload my AppDomain, it still exists in the main
AppDomain.

<snip>
AppDomainSetup setup = new AppDomainSetup();

setup.ApplicationName = "Runner";
setup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;
setup.PrivateBinPath = AppDomain.CurrentDomain.BaseDirectory;
setup.ShadowCopyDirectories = AppDomain.CurrentDomain.BaseDirectory;
setup.ShadowCopyFiles = "true";
setup.CachePath = AppDomain.CurrentDomain.BaseDirectory + "Bin";

AppDomain domain = AppDomain.CreateDomain("RunnerDomain", null, setup);
Assembly ass = domain.Load("Utilities");

Type lType = ass.GetType("Utilities.Gender");
int val = (int)Enum.Parse(lType, "Male");

MessageBox.Show(val.ToString());

AppDomain.Unload(domain);
</snip>

As soon as I call the line:
Type lType = ass.GetType("Utilities.Gender");
My exe's AppDomain loads the Utilities DLL too. So when I unload my
AppDomain, and load it again it keeps the old values from the
Enumeration instead of loading the new one. I am confused. How do I
write my code so that only my RunnerDomain has a copy of Utilities.dll
loaded?

TIA,

Colin Svingen
 
D

Dmitriy Lapshin [C# / .NET MVP]

Hi Colin,

I believe the only way is to never use types from the shadow-copied DLL
directly in the main AppDomain. Introduce some wrapper, or bridge, that will
insulate the main AppDomain from that assembly.
 

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