8

我正在编写一个小型插件库,它使用应用程序域来隔离使用 .Net 框架 4.0 的插件。因此,每个插件中的代码都超出了我的控制范围。当其中一个插件引发未处理的异常时,我观察到结果好坏参半。它们如下。

当插件的主线程中抛出未处理的异常时,调用插件的执行方法的主可插拔应用程序能够干净地捕获并处理它。那里没有问题。然而,

  1. 如果插件在插件的 Execute 方法中为基于 WinForms 的应用程序启动消息循环,并且在 WinForm 应用程序中抛出未处理的异常(即在表单中),则可插入应用程序只能在从 Visual Studio 调试器内部运行时捕获异常. 否则(当在 VS 外部调用时)主可插拔应用程序与插件一起崩溃。

  2. 如果未处理的异常是在插件的 Execute 方法产生的单独线程中引发的,那么可插入应用程序就没有机会捕获异常并崩溃。

我在下面的链接中创建了一个简单的 VS 2010 项目来模拟这种行为。 http://www.mediafire.com/file/1af3q7tzl68cx1p/PluginExceptionTest.zip

其中,可插拔应用程序中的主要方法如下所示

namespace PluginExceptionTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Press enter to load plugin");
            Console.ReadLine();

            Assembly entryAsm = Assembly.GetEntryAssembly();
            string assemblyFileName = Path.Combine(Path.GetDirectoryName(entryAsm.Location), "EvilPlugin.exe");


            AppDomainSetup domainSetup = new AppDomainSetup();
            AppDomain domain = AppDomain.CreateDomain("PluginDomain", null, domainSetup);
            PluginBase plugin = (PluginBase)domain.CreateInstanceFromAndUnwrap(assemblyFileName, "EvilPlugin.Plugin");

            Console.WriteLine("Plugin Loaded.");

            //SCENARIO 1: WinForms based plugin
            Console.WriteLine("Press Enter to execute winforms plugin. (Remember to click on the button in the form to raise exception)");
            Console.ReadLine();

            try
            {
                plugin.ExecuteWinApp();
            }
            catch (Exception)
            {
                //The exception is caught and this gets executed only when running in visual studio debugger. Else application exits. Why?
                Console.WriteLine("WinForms plugin exception caught. However same does not happen when run out of visual studio debugger. WHY?");
            }

            //SCENARIO 2: WinForms based plugin
            Console.WriteLine("Press Enter to execute threading plugin, wait for 3 seconds and the app will exit. How to prevent app from exiting due to this?");
            Console.ReadLine();
            try
            {
                plugin.ExecuteThread();
            }
            catch (Exception)
            {
                //This never gets executed as the exception is never caught. Application exits. Why?
                Console.WriteLine("WinForms plugin exception caught");
            }

            Console.ReadLine();
        }
    }
}

这是插件项目的代码。它继承自上述可插拔应用程序项目中的 PluginBase 类。

namespace EvilPlugin
{
    public class Plugin:PluginBase
    {
        public Plugin():base()
        {

        }

        public override void ExecuteWinApp()
        {            
            Application.Run(new Form1());            
        }

        public override void ExecuteThread()
        {
            Thread t = new Thread(new ThreadStart(RaiseEx));           
            t.Start();
        }

        private void RaiseEx()
        {
            Thread.Sleep(3000);
            throw new Exception("Another Evil Exception in a seperate thread");
        }
    }
}

最后这是插件中表单的代码

namespace EvilPlugin
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnException_Click(object sender, EventArgs e)
        {
            throw new Exception("Evil Exception");
        }
    }
}

由于上述两种情况(1和2),我如何防止主进程退出?

提前致谢。

4

1 回答 1

1

用于处理 WinForms 异常。您可以在 PluginBase 类中为此类 ThreadExceptions 设置“陷阱”:

public abstract class PluginBase:MarshalByRefObject
{
    protected PluginBase()
    {
        System.Windows.Forms.Application.ThreadException +=
            (o, args) =>
            {
                throw new Exception("UntrappedThread Exception:" + args.Exception);
            };

    }

    public abstract void ExecuteWinApp();
    public abstract void ExecuteThread();
}

默认情况下,它会显示“异常窗口”。但如果提供,将得到这个处理程序。

至于从其他线程捕获异常。不可能。您能做的最好的事情就是收到有关抛出异常的通知。

    AppDomain domain = AppDomain.CreateDomain("PluginDomain", null, domainSetup);
    domain.UnhandledException +=
        (o, eventArgs) =>
            {
                Console.WriteLine("Exception was caught from other AppDomain: " + eventArgs.ExceptionObject);
                Console.WriteLine("CLR is terminating?: " + eventArgs.IsTerminating);
            };
于 2011-06-30T07:13:58.790 回答