views:

366

answers:

2

I am writing a WinForms application that accepts dynamic user interface controls, in the form of plugins (think widgets). For this, the main function of each plugin returns a UserControl that is then added to the main form.

Since my application doesn't have direct control over them, I would like to "sandbox" the plugins exception-wise such that if one of the UserControls throws an error, I can catch it and unload the culprit. The obvious trouble is that the UserControls are event driven and can throw exceptions in a million different places, so I don't have one thing to wrap in a try/catch block.

How can I catch the exceptions thrown by dynamic UserControls or what would be a better design for such a modular application?

+1  A: 

you can use a global exception handling (like a custom HTTPModule) And then determine the exception was throw by a which plug in.

take a look to this links, maybe useful
http://www.15seconds.com/issue/030102.htm
http://codebetter.com/blogs/karlseguin/archive/2006/06/12/146356.aspx

Nima
Guess I omitted an important detail: this is a WinForms application, not ASP (edited now). In either case, the global exception handling idea might be a way out, so I'm looking into some AOP alternatives. Thanks.
Tiberiu Ana
+1  A: 

Encapsulate your UserControl within an object implementing your plugin interface by simply calling the plugin implementation and adding your try catch logic around each call.

François
I'm not sure I see what you mean. The plugin interface has only one function: GetUserControl(). After that, the UserControl is added to the form and driven by UI events.
Tiberiu Ana
ok... UserControl is a bit complicated.I don't know if it work for visual constrols but can you load your user control in another AppDomain and register to event appDomain.UnhandledException.
François
A-ha, now you're talking. Unfortunatelly it doesn't seem possible to use UserControls from another AppDomain, but googling for AppDomain separation got me some pages on .NET plugins that I hadn't seen before. Thanks.
Tiberiu Ana