Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unloading AppDomains

Tags:

.net

appdomain

Supose I have AppDomainA, which spins up AppDomainB. AppDomainB then spins up AppDomainC.

If, within AppDomainA I unload AppDomainB, does AppDomainC also get unloaded or must I make sure to handle that on my own?

like image 355
Anthony Compton Avatar asked Mar 28 '14 12:03

Anthony Compton


1 Answers

Best way to find out is to try it. Here is an example of creating AppDomainA, which creates AppDomainB. We tell B to do some work, and unload A.

internal class Program
{
    private static Timer _timer;
    private static void Main(string[] args)
    {
        var domainA = AppDomain.CreateDomain("AppDomainA");
        domainA.DomainUnload += (s, e) => Console.WriteLine("DomainA was unloaded.");
        domainA.DoCallBack(() =>
        {
            var domainB = AppDomain.CreateDomain("AppDomainB");
            domainB.DomainUnload += (s, e) => Console.WriteLine("DomainB was unloaded.");
            domainB.DoCallBack(() =>
            {
                _timer = new Timer(o =>
                {
                    Console.WriteLine("Tick from AppDomain: " + AppDomain.CurrentDomain.FriendlyName);
                }, null, 0, 1000);
            });
        });
        AppDomain.Unload(domainA);
        Application.Run(); //Run a message loop so AppDomainB can keep doing work.
    }
}

We see we get the message AppDomainA was unloaded, but not B, and B keeps working. Our conclusion is you need to make sure to handle this on your own.

like image 74
vcsjones Avatar answered Oct 06 '22 00:10

vcsjones