良好的架构/图书馆用于强大的插件/插件管理 [英] Good Architecture/Library For Robust Plugin/Addon Management

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

问题描述

我们有一个应用程序,作为其要求之一,将采取任意的第三方插件,加载它们,并在我们本土的应用程序旁边运行他们的UI。我们已经将这些第三方插件加载到自己的AppDomains中进行隔离,一切正常。



直到其中一个插件因未处理的异常而崩溃。在这种情况下,整个应用程序都会下降,即使真正受到影响的是我们的额外工具窗口之一。



我们希望理想的方法是处理未处理的异常,卸载受损的AppDomain,然后重新加载它。 >问题是我们在未处理的异常的事件处理程序中找不到一个机制,我们可以将异常标记为被处理。此外,由于插件具有自己的用户界面组件与用户自己的一组交互,所以在try / catch / finally块中与我们的插件交换我们的交互是非常困难的。



有没有任何框架/编程库/模式可以解决这个问题?我们可以做插件的东西很好

解决方案

我们需要帮助的是保持应用程序存活,不同AppDomain中的代码意外失败。 >您可以使用 System.Addin 框架(有时称为 MAF ),这有点麻烦设置正确,但是旨在提供隔离(防撞保护)。 System.Addin 是基于远程处理。使用此框架,您可以让插件在有限的权限,同一进程或另一个应用程序域中运行,甚至在其他进程中运行。



如果您需要完全崩溃保护您可能需要使用过程分离选项。



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

  AppDomain addInDomain = AppDomain.CreateDomain(addin domain); 

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

//激活加载项
IHostView addinInstance = addinToken.Activate< IHostView>(env);

Console.WriteLine(addinInstance.DoSomething());

AppDomain.Unload(addInDomain);

如果你想加载插件到另一个进程,为了完全隔离:

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

//激活加载项
IHostView addinInstance = addinToken.Activate< IHostView>(process,AddInSecurityLevel.Internet);

尝试
{
//使用一个catch块,防止插件崩溃主要应用程序的异常
Console.WriteLine(addinInstance.DoSomething());
}
catch(异常e)
{
Console.WriteLine(e);
}

process.Shutdown();

这个博客给出了一个很好的设置说明。



可以将 System.Addin 与MEF相结合,这些是免费的工具包,请参阅本文



请注意,System.Addin模型可能提供防碰撞保护,您仍然需要处理加载代码中的减速或死锁。异步使用将在这里有所帮助。


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.

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.

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.

解决方案

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.

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();

This blog gives a good description of setting this up.

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

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天全站免登陆