Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I isolate my current AppDomain from being torn down, when another AppDomain I've loaded throws an unhandled exception?

Tags:

c#

.net

appdomain

Probably a copy of: Can I prevent an uncaught exception in another AppDomain from shutting down the application?

Been trying all day to figure out the answer to this question.

Just want to make sure that the answer is indeed no, before I throw away all the code Ive made to isolate my drivers inside their separate appdomains and replace it with old-school processes.

So the formal question is this.

Having a default domain "ad-default", in which I create a new appdomain "ad-hosted", can I avoid that unhandled exceptions from "ad-hosted" tears down "ad-default"?

I am aware that I can observe the exceptions by hooking up to the UnhandledException event of the "ad-hosted" domain, but I can find no way to stop them from propogating to the "ad-default" domain.

Is this really true? But why would we even want AppDomain's, if they do not provide isolation?

EDIT: The answer is unfortunately NO, see this answer for explanation: AppDomain, handling the exceptions

like image 218
Casper Leon Nielsen Avatar asked Oct 22 '11 17:10

Casper Leon Nielsen


1 Answers

The only way to isolate the exceptions in a thread in another appdomain from tearing down the default domain is to use:

 <runtime>
    <legacyUnhandledExceptionPolicy enabled="1"/>
 <runtime>

Which will set IsTerminating flag of the unhandledexception to false, and prevent the default domain from shutting down.

What we did in our case was to hook up to the UnhandledExceptionHandler in both domains. We then trigger a semafore in in the "ad-hosted" that will be picked up by a thread created for this purpose in "ad-default", which then in turn disposes the "ad-hosted"

This is a hack and will probably not survive into future generations of the framework, but it makes "ad-default" more robust, in that it will not be torn down on unhandled exceptions in the "ad-hosted"

we also hook up a eventhandler in "ad-default" that will cast sender as appdomain and check if the exception originated in "ad-default", if so we tear down the "ad-default" too, emulating the behavior of .net 2.0, even with the runtime setting described above.

Hope this gives somebody a clue on how to make a more resilent plugin host.

like image 67
Casper Leon Nielsen Avatar answered Nov 15 '22 07:11

Casper Leon Nielsen