Robust C# Plugin System
I am writing a tool which communicates with more than one version control system, either subversion or clearcase, and has various utilities which can be plugged into it. I though of using MEF to load the version control providers, and utiilities, both of which have interfaces definied for them.
I've started to use MEF for this. But if loading any plugin fails (on half my clients machin开发者_如何学运维es, the ClearCase COM bindings won't be present, so loading will fail, on the other half SVN won't be installed, so the SVN plugin won't load) though, MEF throws an exception and no plugin works; I was hoping that the rest of the plugins would load.
[ImportMany(AllowRecomposition = true)]
public IEnumerable<IVersionControl> RegisteredProviders { get; private set; }
Instead, if any providers fail to construct themselves, then RegisteredProviders
is null.
Is there a simple way of configuring MEF to achieve this (reporting exceptions but loading other instances), or do I have to change the interface to delay their intialisation failure until after MEF has instantiated the objects?
It depends on exactly when the exception is thrown. You may be able to get what you want by changing your import to this:
[ImportMany(AllowRecomposition = true)]
public IEnumerable<Lazy<IVersionControl>> RegisteredProviders { get; private set; }
That might delay the exception until you access the Value property of the Lazy.
It might be better to write your version control providers so that they can successfully load even if the required dependencies aren't present, and then put a property on them which will check if the dependency is present.
精彩评论