好的建筑/库强大的插件/插件管理 [英] Good Architecture/Library For Robust Plugin/Addon Management

查看:195
本文介绍了好的建筑/库强大的插件/插件管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个应用程序,正如它的要求之一,将任意的第三方插件,加载它们,并运行其UI与我们的本土应用。我们一直在加载这些第三方插件到自己的应用程序域隔离的目的,一切工作正常。

We have an application which, as one of its requirements, will take arbitrary 3rd party plugins, load them, and run their UI alongside our home-grown application. We've been loading these 3rd party plugins into their own AppDomains for isolation purposes, and everything works ok.

直到任一插件崩溃了与未处理的异常。在这种情况下,整个应用程序出现故障,即使所有的真正影响是我们的额外的工具窗口之一。

Until one of the plugins crashes out with an unhandled exception. In this circumstance, the entire application goes down, even though all that is really affected is one of our 'extra' tool windows.

我们希望,理想情况下,一些方法来处理未处理异常,卸载受损的AppDomain,然后只需重新加载它的新鲜。的问题是,我们能不能​​找到一种机制在事件处理未处理的异常,由此我们可以标记除了为被处理。此外,由于插件有自己的UI组件用自己的一套与用户的交互,这将是非常困难的包装我们用的try / catch / finally块插件的互动。

We'd like, ideally, some way to handle the "unhandled" exception, unload the damaged AppDomain, and then just reload it fresh. The problem is that we can't find a mechanism in the event handler for the unhandled exception whereby we can flag the exception as being 'handled'. Further, since the plugins have their own UI components with their own set of interactions with the user, it would be extremely difficult to "wrap" our interactions with the plugins in try/catch/finally blocks.

是否有借自己来解决这个问题的任何框架/编程库/模式?我们可以做插件的东西罚款; 我们需要的帮助是保持应用程序活着的时候,code在不同的AppDomain中意外失败。

Are there any frameworks/programming libraries/patterns that lend themselves to solving this problem? We can do plugin stuff fine; what we need help with is keeping the application alive when code in a different AppDomain fails unexpectedly.

推荐答案

您可以使用 System.Addin 框架(有时被称为 MAF ),这是一个有点麻烦的正确设置,但其目的是提供隔离(碰撞保护)。 System.Addin 基于远程处理。有了这个框架可以让插件具有有限权限运行,在同一个过程中,或者在其他应用域,或甚至在另一个过程

You can use the System.Addin framework (sometimes known as MAF), which is a bit of a hassle to set up correctly, but which was designed to provide isolation (crash protection). System.Addin is based on remoting. With this framework you can let plugins run with limited permissions, in the same process, or in another app-domain, or even in another process.

如果您需要总的碰撞保护,您可能需要使用过程中的分离选项。它可能会在性能,虽然成本。

If you need total crash protection you may need to use the process separation option. It may come at the cost of performance though.

您可以使用此code加载插件在不同的应用程序域:

You can use this code to load an addin in a different app domain:

AppDomain addInDomain = AppDomain.CreateDomain("addin domain");

// addInDomain.PermissionSet = ...
AddInEnvironment env = new AddInEnvironment(addInDomain);

// Activate the add-in
IHostView addinInstance = addinToken.Activate<IHostView>(env);

Console.WriteLine(addinInstance.DoSomething());

AppDomain.Unload(addInDomain);

如果您想要加载的插件到另一个进程中,完全隔离:

If you want to load the addin into another process, for complete isolation:

AddInProcess process = new AddInProcess();
process.Start();

// Activate the add-in
IHostView addinInstance = addinToken.Activate<IHostView>(process, AddInSecurityLevel.Internet);

try 
{
    // use a catch block, prevent exceptions from the addin crashing the main app
    Console.WriteLine(addinInstance.DoSomething());
} 
catch (Exception e)
{
    Console.WriteLine(e);
}

process.Shutdown();

这<一href="http://blogs.microsoft.co.il/blogs/bursteg/archive/2007/08/13/Build-AddIns-with-System-AddIn.aspx"相对=nofollow>博客使设置此功能的一个很好的说明。

This blog gives a good description of setting this up.

这是可以组合 System.Addin 与MEF,这些都是免费的工具包,看到的这篇文章

It is possible to combine System.Addin with MEF, these are complimentary toolkits, see this article.

请注意,该System.Addin模型可提供碰撞保护,你仍然需要处理的插件code减速或死锁。异步使用量将有所帮助。

Note that the System.Addin model may provide crash protection, you will still need to deal with slowdowns or deadlocks in the addin code. Asynchronous usage will help here.

这篇关于好的建筑/库强大的插件/插件管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆