c# - Application crash when anoter domain throws exception -


i'm studing c#. read books of andrew troelsen "c# , .net platform" , jeffrey richter's "clr via c#". now, i'm trying make application, load assemblies directory, push them appdomain's , run method's included (the application supports plug-ins). here dll common interface. add application, , in dlls plugins. mainlib.dll

namespace mainlib { public interface icommoninterface {     void showdllname(); } } 

here plugins: pluginwithoutexception

namespace pluginwithoutexception { public class withoutexception : marshalbyrefobject, icommoninterface {     public void showdllname()     {         messagebox.show("pluginwithoutexception");     }      public withoutexception()     {      } } } 

and one: pluginwithexception

namespace pluginwithexception { public class withexception : marshalbyrefobject, icommoninterface {     public void showdllname()     {         messagebox.show("withexception");         throw new notimplementedexception();     } } } 

and here application, loads dlls , runs them in appdomain's

namespace plug_inapp {    class program {      static void main(string[] args)     {          threadpool.queueuserworkitem(createdomainandloadassebly, @"e:\plugins\pluginwithexception.dll");          console.readkey();     }     public static void createdomainandloadassebly(object name)     {         string assemblyname = (string)name;         assembly assemblytoload = null;         appdomain domain = appdomain.createdomain(string.format("{0} domain", assemblyname));         domain.firstchanceexception += domain_firstchanceexception;          try         {             assemblytoload = assembly.loadfrom(assemblyname);         }         catch (filenotfoundexception)         {             messagebox.show("can't find assembly!");             throw;         }          var theclasstypes = t in assemblytoload.gettypes()                             t.isclass &&                                   (t.getinterface("icommoninterface") != null)                             select t;         foreach (type type in theclasstypes)         {             icommoninterface instance = (icommoninterface)domain.createinstancefromandunwrap(assemblyname, type.fullname);             instance.showdllname();         }      }      static void domain_firstchanceexception(object sender, system.runtime.exceptionservices.firstchanceexceptioneventargs e)     {         messagebox.show(e.exception.message);     } } } 

i expect, if run instance.showdllname(); in domain (maybe wrong?) unhandled exception drop domain runs, default domain work. in case - default domain crashes after exception occurs in domain. please, tell me i'm doing wrong?

there way control on if need to. because our add-ins can written outside of our team, , as possible try prevent our application crashing because of other people's add-ins.

so our application take down appdomain threw exception, inform user, , carry on. or failfast if exception came main appdomain.

in app.config need following:

<configuration>  <runtime>   <legacyunhandledexceptionpolicy enabled="true" />  </runtime> </configuration> 

this revert legacy behaviour unhandled exceptions , allow decide whether kill entire process or appdomain.

you still have other issues deal with, such working out exceptions came appdomain.

another issue not exceptions serializable, means turn serializationexception when cross appdomain boundary.

because our add-ins implement common base class got around these issues putting unhandled exception handlers in add-ins themselves. hook appdomain.currentdomain.unhandledexception , taskscheduler.unobservedtaskexception , call appdomain.unload(appdomain.currentdomain) terminate add-in.

this isn't perfect, works our project.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -