c# - 处理使用 MAF 创建的插件 (System.AddIn)

标签 c# garbage-collection dispose maf system.addin

有谁知道如何处理使用 System.AddIn 创建的插件。所有在线示例似乎都展示了如何轻松加载和使用插件,但没有一个展示了如何在它们还活着时处理它们。我的问题是我在新进程中创建插件,而这些进程从来没有被垃圾收集,这显然是一个问题。

下面是一些示例代码来说明我的问题。假设用户从未退出此应用程序,而是创建了多个 ICalculator 实例。这些插件进程是如何处理掉的?

    static void Main(string[] args)
    {
        string addInRoot = GetExecutingDirectory();

        // Update the cache files of the pipeline segments and add-ins
        string[] warnings = AddInStore.Update(addInRoot);

        // search for add-ins of type ICalculator
        Collection<AddInToken> tokens = AddInStore.FindAddIns(typeof(ICalculatorHost), addInRoot);

        string line = Console.ReadLine();
        while (true)
        {
            AddInToken calcToken = ChooseCalculator(tokens);

            AddInProcess addInProcess = new AddInProcess();
            ICalculatorHost calc = calcToken.Activate<ICalculatorHost>(addInProcess, AddInSecurityLevel.Internet);

            // run the add-in
            RunCalculator(calc);    
        }
    }

最佳答案

我设法找到了上述问题的解决方案,它利用了 AddInController 类及其关闭方法。现在看看我是否可以在我的应用程序中使用它,而不仅仅是这个例子:

    static void Main(string[] args)
    {
        string addInRoot = GetExecutingDirectory();
        string[] warnings = AddInStore.Update(addInRoot);
        Collection<AddInToken> tokens = AddInStore.FindAddIns(typeof(ICalculatorHost), addInRoot);


        while (true)
        {
            AddInToken calcToken = ChooseCalculator(tokens);

            AddInProcess addInProcess = new AddInProcess();
            ICalculatorHost calc = calcToken.Activate<ICalculatorHost>(addInProcess, AddInSecurityLevel.Internet);

            // run the add-in
            RunCalculator(calc);

            // shutdown the add-in when the RunCalculator method finishes executing
            AddInController controller = AddInController.GetAddInController(calc);
            controller.Shutdown();
        }
    }

关于c# - 处理使用 MAF 创建的插件 (System.AddIn),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3880651/

相关文章:

c# - SignalR HubConnection 未从无服务器 Azure SignalR 服务接收消息

Java垃圾收集器

java - 什么时候创建/销毁常量字符串?

winforms - 我应该处置 HtmlElement 实例吗?

vb.net - 我是否需要释放 COM Interop 对象的内部对象?

c# - Xamarin PCL 使用 443 以外的其他端口与 REST Api 通信

c# - 将 IEnumerable<T> 转换为 List<SelectListItem> 的 Lambda 扩展方法

c# - 同一节点上的 Xpath 多个条件

language-agnostic - 学习垃圾收集理论

c# - 返回用于在 using C# 中使用的变量