C# 处置使用MAF(System.AddIn)创建的加载项

C# 处置使用MAF(System.AddIn)创建的加载项,c#,garbage-collection,dispose,maf,system.addin,C#,Garbage Collection,Dispose,Maf,System.addin,是否有人知道如何处置使用System.AddIn创建的加载项。所有在线示例似乎都展示了如何轻松加载和使用加载项,但没有一个演示了如何在加载项还活着时处理它们。我的问题是我在新进程中创建插件,而这些进程从未被垃圾收集,这显然是个问题 下面是一些示例代码,说明了我的问题。假设用户从未退出此应用程序,而是创建了许多ICalculator实例。这些加载项进程是如何处理的 static void Main(string[] args) { string addInRoot

是否有人知道如何处置使用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);    
        }
    }
static void Main(字符串[]args)
{
字符串addInRoot=GetExecutingDirectory();
//更新管道段和加载项的缓存文件
string[]warnings=AddInStore.Update(addInRoot);
//搜索ICalculator类型的加载项
集合令牌=AddInStore.FindAddIns(typeof(ICalculatorHost),addInRoot);
string line=Console.ReadLine();
while(true)
{
AddInToken calcToken=选择计算器(代币);
AddInProcess AddInProcess=新的AddInProcess();
iCalCalculatorHost calc=calcToken.Activate(addInProcess,AddInSecurityLevel.Internet);
//运行外接程序
运行计算器(calc);
}
}

我设法找到了上述问题的解决方案,它利用了AddInControl类及其shutdown方法。现在,看看我是否能在我的应用程序中实现这一点,而不仅仅是这个示例:

    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();
        }
    }
static void Main(字符串[]args)
{
字符串addInRoot=GetExecutingDirectory();
string[]warnings=AddInStore.Update(addInRoot);
集合令牌=AddInStore.FindAddIns(typeof(ICalculatorHost),addInRoot);
while(true)
{
AddInToken calcToken=选择计算器(代币);
AddInProcess AddInProcess=新的AddInProcess();
iCalCalculatorHost calc=calcToken.Activate(addInProcess,AddInSecurityLevel.Internet);
//运行外接程序
运行计算器(calc);
//当RunCalculator方法完成执行时关闭外接程序
AddInControl控制器=AddInControl.GetAddInControl(计算);
controller.Shutdown();
}
}

您的解决方案对我不起作用

因为您正在为您的加载项创建一个新流程,所以您可以

addInProcess.Shutdown();

外部进程将关闭

哦,一些额外信息。我还在我的HostViewAdapter类中保留一个对契约句柄的引用,它似乎仍然没有起到作用。+1用于报告您自己的解决方案。非常本着这样的精神。谢谢,我想我会让我的开发伙伴们免于痛苦“我想我会让我的开发伙伴们免于痛苦”-系统带来了很多痛苦。AddIn。。。。