Catching Parts Exceptions

Mar 9, 2010 at 10:43 AM

Hi,

I am using MEF and I cannot find an easy way to catch exceptions coming from my parts. How can I catchmy exception thrown from a part component in my hostcontainer ? The idea is to make my application robust. Today, I can only catch those exception in the UnhandledException handler, but the thing is, CLR will always be Terminating after that,which is not what I'm looking for. I would like to be capable of catching an exception from my components, in order to log it, then unload my component to reload it at its default state.

 

Does anyone has an idea on managing Parts Exception in the Host Container?

 

Thanks for your ideas,

Pierre L.

Mar 9, 2010 at 1:15 PM
Edited Mar 9, 2010 at 1:17 PM

If you can put the 3rdparty components in a separate catalog, you could try to fall back to a "safe mode" without that catalog like this:

public static void Main(string[] args)
{
   CompositionContainer container;
   Program program;
   try
   {
      container = GetFullContainer();
      program = container.GetExportedValue<Program>();
   }
   except (SomeException e)
   {
      LogSomething(e);
// some logic should be added here to determine whether // it is desirable to attempt starting the program without // plugins, e.g. prompt the user container = GetSafeModeContainer(); program = container.GetExportedValue<Program>(); } program.Run(); } private static CompositionContainer GetFullContainer() { var pluginCatalog = new DirectoryCatalog(@".\plugins"); var pluginExportProvider = new CatalogExportProvider(pluginCatalog); var mainCatalog = GetMainCatalog(); var mainExportProvider = new CatalogExportProvider(mainCatalog); var container = new CompositionContainer(
pluginExportProvider, mainExportProvider); pluginExportProvider.SourceProvider = container; mainExportProvider.SourceProvider = container; return container; } private static CompositionContainer GetSafeModeContainer() { var mainCatalog = GetMainCatalog(); return new CompositionContainer(mainCatalog); }
 

 

 

Mar 9, 2010 at 1:27 PM

That would be a solution for a crash recovery wouldn't it be? I mean, it will only affect my application in case it cannot load again, because of a 3rd party component. (Just whipping it from my catalog would do the same, wouldn't it?). But that's not what I'm looking for. I just don't want to see my application crash because of a 3rd part component. The idea is "Component Isolation" if you prefer, more than crash recovery. I am looking for a way to intercept exceptions "Host Side", or something that would do the same trick.

I chatted today with a good friend of mine, expert in MEF, and he told me that what I'm trying is impossible, but someone out there might have a great idea, or got the same issue ;)

Anyway, thank you for your quick answering, very nice of you :-)

If you see an answer to my question, please notify this post ;)

Pierre L.

 

Mar 9, 2010 at 4:46 PM

Remoting / System.Addin may be a soluton.

Mar 10, 2010 at 4:35 AM

I'm not sure that this is your scenario, but I think that if your looking for robust "parts" in order to prevent say unhandled exceptions in 3rd-party author "extensions" from bringing down your host application, you need to introduce a host framework layer that manages interaction with those extensions.

One thing you could consider is having named methods in an "extension" abstract base class like Initialize(), Started() etc. that are implemented in extensions (Parts), and called through your framework layer.  Then catch any thrown exceptions in your framework.

This scheme has proven very successful in a real-world application.

-- Alex Hoffman

 

 

Mar 10, 2010 at 9:15 AM

Thank you all for your answers!

@alexh : This is exactly my scenario. Indeed your solution could be a solution. I was doing something like that in fact in my first tests, but it seemed not strong enough, that is, you could always find ways to crash an extensions without being easily capable of managing all its resources. The solution I found was to remodeled the entire architecture and have a Multi Process Application, that is every thing should be an application, and my platform shall work as an "OS" for those processes. Of course, by OS I only mean monitoring processes, being able to detect crashes, easily unload and reload them if needed, etc. Thank you for your answer. All the best for your work and futur. ;)

@gblock : Hi Gblock, thank you too for answering (both my posts ;)). As you can read in my answer to alexh, I didn't choose the System.Addin solution (nor remoting), because the complexity of having both MEF and MAF makes it quite hard to have a robust and easy developping SDK for my platform (plus I'm not truly convinced by MAF. working myself with AppDomains was often more productive, and better optimized). Still, thank you very much for your attention.

Having choosen a solution, I thank you all for your answers, and wish the best for you all.

 

Pierre L.