如何围绕MefBootstrapper InitializeModules实现异常处理?

问题描述 投票:0回答:1

我有一些棱镜工作。特别是,一个调用InitializeModules的引导程序(MefBootstrapper)。在其中一个模块中,会引发异常,当我重新抛出此异常时,我得到一个未处理的异常。

不成功,我已将委托方法添加到异常事件中,例如:

AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException;
System.Windows.Application.Current.DispatcherUnhandledException += CurrentOnDispatcherUnhandledException;
exception-handling prism
1个回答
0
投票

首先,您需要将附加到AppDomain.CurrentDomain.UnhandledException的事件处理程序中处理的异常标记为防止应用程序崩溃:

Application.Current.Dispatcher.UnhandledException += (sender, e) => e.Handled = true;

其次,在给定的Prism模块初始化期间抛出的异常可以阻止其他模块加载。为了避免这种情况,您可以按如下方式继承ModuleManager:

public class ErrorHandlingModuleManager : ModuleManager
{
    public ErrorHandlingModuleManager(IModuleInitializer moduleInitializer, IModuleCatalog moduleCatalog, ILoggerFacade loggerFacade) : base(moduleInitializer, moduleCatalog, loggerFacade)
    {
    }

    protected override void LoadModulesThatAreReadyForLoad()
    {
        var initializationExceptions = new List<Exception>();

        while (true)
        {
            try
            {
                base.LoadModulesThatAreReadyForLoad();

                break;
            }
            catch (ModuleInitializeException e)
            {
                initializationExceptions.Add(e);
            }
            catch (Exception e)
            {
                initializationExceptions.Add(e);

                break;
            }
        }

        if (initializationExceptions.Any())
            throw new AggregateException(initializationExceptions);
    }
}

}

务必使用Mef容器注册ErrorHandlingModuleManager以覆盖默认值。

© www.soinside.com 2019 - 2024. All rights reserved.