AppDomains, Unloading, and ThreadAbortException

2019-07-29 05:16发布

I'm trying to work with AppDomains for the first time and I'm finding myself a bit lost.

Here is what I've done: I have a console app that instantiates a Bootstrapper class and calls Bootstrapper.Bootstrap.

The class looks something like this:

    public class Bootstrapper : MarshalByRefObject
    {
        private static AppDomain SecondaryAppDomain;
        private Bootstrapper _secondaryDomainBootstrapper;
        public Robot CurrentlyRunningRobot;
        public Bootstrapper OwningBootstrapper;

        public Bootstrapper()
        {

        }

        public void Bootstrap()
        {
            InitializeSecondaryAppDomain();
            RunInSecondaryAppDomain();
        }

        private void DestroySecondaryAppDomain()
        {
            AppDomain.Unload(SecondaryAppDomain);
        }

        private static int initCount = 0;

        private static void InitializeSecondaryAppDomain()
        {
            initCount++;
            SecondaryAppDomain = AppDomain.CreateDomain("SecondaryAppDomain" + initCount);
        }

        private void RunInSecondaryAppDomain()
        {
            _secondaryDomainBootstrapper =
                (Bootstrapper)
                    SecondaryAppDomain.CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName,
                        "myNamespace.Bootstrapper");
            _secondaryDomainBootstrapper.OwningBootstrapper = this;
            _secondaryDomainBootstrapper.Initialize(Args);
        }        

        private void Initialize(string[] args)
        {
            //Do some stuff...
            //Start() returns Task<Robot>
            var robot = Initializer.Start();
            CurrentlyRunningRobot = robot.Result;
            CurrentlyRunningRobot.HardResetRequested += OnHardResetRequested;
            robot.Wait();
        }

        private void DoHardReset()
        {
            DestroySecondaryAppDomain();
            InitializeSecondaryAppDomain();
            RunInSecondaryAppDomain();
        }

        private void OnHardResetRequested(object sender, EventArgs e)
        {
            OwningBootstrapper.DoHardReset();
        }
    }

The intention is that whatever is running in the Secondary domain and request that it be terminated and restarted.

What's happening, though, is that when I call DestroySecondaryAppDomain() (from within the default AppDomain) I wind up with ThreadAbortExceptions.

I've been reading a bunch of docs and that seems totally normal. The thing that I'm having difficulty with is why I can't seem to deal with it in my default AppDomain.

When the secondary AppDomain is unloaded (in DestroySecondaryAppDomain) I never get to execute the rest of the code in DoHardReset. What (probably simple) thing am I failing to understand?

1条回答
来,给爷笑一个
2楼-- · 2019-07-29 05:55

To make a long story short, there was still code executing in that AppDomain. It needed to be completely stopped before it can be successfully unloaded without error.

查看更多
登录 后发表回答