开发者

Send message to original App Domain

开发者 https://www.devze.com 2023-01-31 18:36 出处:网络
I have an application that creates a new app domain like this: private static AppDomain domain = null;

I have an application that creates a new app domain like this:

private static AppDomain domain = null;

private static void LoadAndLaunchAppDomain(string assemblyFile, string typeName)
{
    AppDomainSetup setup = new AppDomainSetup()
    {
        ApplicationBase = AppDomain.CurrentDomain.BaseDire开发者_开发知识库ctory,
        ShadowCopyFiles = "true"
    };

    domain = AppDomain.CreateDomain("ClientKernel", null, setup);
    domain.UnhandledException += new UnhandledExceptionEventHandler(domain_UnhandledException);
    ClientKernelLauncher launcher = (ClientKernelLauncher)domain.CreateInstanceFromAndUnwrap(assemblyFile, typeName);
    launcher.Launch();
}

static void domain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
    // handler
}

At some point an exception is thrown and the flow reaches inside the HANDLER. What I actually want is to recreate the domain when this happens. What I noticed is that the handler is actually running on the "ClientKernel" app domain and not the original domain that created it.

How can communicate back to the original domain and tell it to call again the LoadAndLaunchAppDomain() method?


Use AppDomain.DoCallBack() to call a method on another AppDomain.

I am not sure if that will work once an unhandled exception is thrown in calling AppDomain. But, you could give it a shot.


Would something like this work?

AppDomain hostDomain = AppDomain.CurrentDomain;
domain = AppDomain.CreateDomain("ClientKernel", null, setup);
domain.UnhandledException += (s, e) => {
   hostDomain.DoCallBack(() => { SomeStaticClass.LoadAndLaunchAppDomain("someAssembly", "someClassName"); }
}

I don't know what the name of your class hosting the code is so you'll need to change SomeStaticClass to the right class name.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号