0

我使用MAF,但在激活插件时遇到问题。让我解释一下我的计划。

首先我创建一个辅助 AppDomain 然后我尝试激活插件:

MyAddIn = Token.Activate<AddInHostView>(domain);

我的 AddIn 非常简单,只引用了一个辅助程序集。如果这个助手程序集在 AddIn 的目录中,所有的东西都像一个魅力。

插件

  • MyDemoAddIn.dll
  • 助手.dll

如果我删除 Helpers.dll 整个应用程序崩溃:

MyDemoAddIn.DLL 'PresentationHost.exe' (Managed (v4.0.30319)) 中发生了“System.IO.FileNotFoundException”类型的第一次机会异常:加载了 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\mscorlib.resources \v4.0_4.0.0.0_de_b77a5c561934e089\mscorlib.resources.dll' mscorlib.dll 中出现“System.Reflection.TargetInvocationException”类型的第一次机会异常 System.Reflection.TargetInvocationException 类型的第一次机会异常发生。 AddIn.dll System.AddIn.dll 中出现“System.Reflection.TargetInvocationException”类型的第一次机会异常

我用异常处理测试了所有东西。我无法从主机、AddInView 或我创建的 AppDomain 中捕获异常:-(

有人有想法吗?

4

2 回答 2

0

The answer is easier than you think... The problem was a error in the Finalizer() Method from the AddInView.dll which is an interlayer of the MAF. Here is my solution.

Demo (Host)

try
{
    MyAddIn = Token.Activate<AddInHostView>(domain);
}
catch (Exception ex)
{
    try
    {
        AppDomain.Unload(domain);
        domain = null;
    }
    catch (ThreadAbortException threadAbortException)
    {
        //ToDo: Logging
    }
    catch (CannotUnloadAppDomainException cannotUnloadAppDomainException)
    {
        //ToDo: Logging
    }
    catch (Exception exception)
    {
        //ToDo: Logging
    }
}

AddInView

[AddInBase]
public class AddInView : UserControl
{
    //Necessary constructor to handle the exception.
    //Normal constructor is not called when an error occurs at startup!!!
    static AddInView()
    {
        AppDomain.CurrentDomain.DomainUnload += CurrentDomain_DomainUnload;
    }

    //Normal constructor
    public AddInView()
    {
        //Do other things...
        //e.g. Dispatcher.UnhandledException += Dispatcher_UnhandledException;
    }

    static void CurrentDomain_DomainUnload(object sender, EventArgs e)
    {
        //To cleanup and stuff
    }
}

Now, if an error occurs (Activate<>) the exception handler catches the error (FileNotFound Helpers.dll not found) and unload the whole AppDomain without crash the main AppDomain :-)

于 2011-07-19T12:35:47.683 回答
0

没有 Helpers.dll,您的插件无法运行 因为它跨 AppDomain 边界工作,所以插件需要它自己的一组 DLL 才能加载到自己的 AppDomain 中。

如果您不需要跨 AppDomain 功能,Token.Activate<AddInHostView>(AppDomain.CurrentDomain)如果您在宿主项目中引用了 helpers.dll,则可以使用它来加载它。

于 2011-07-16T14:43:03.500 回答