如何在单独的AppDomain中托管IronPython引擎?

时间:2009-12-01 03:54:20

标签: c# ironpython embedding

我使用以下代码在c#的单独“appDomain”上执行IronPython脚本。 (我用这种方法来解决内存泄漏问题) 花费较少时间(少于3分钟)的脚本执行得很好。

但是如果需要更长时间(超过5分钟)的脚本会抛出一个例外 - > System.Runtime.Remoting.RemotingException:Object'/011b230e_2f28_4caa_8bbc_92fabb63b311/vhpajnwe48ogwedf6zwikqow_4.rem'

using System;
using Microsoft.Scripting;

namespace PythonHostSamle
{
  class Program
  {
    static void Main(string[] args)
    {
        AppDomain sandbox = AppDomain.CreateDomain("sandbox");
        var engine = IronPython.Hosting.Python.CreateEngine(sandbox);
        var searchPaths = engine.GetSearchPaths();
        searchPaths.Add(@"C:\Python25\Lib");
        searchPaths.Add(@"C:\RevitPythonShell");
        engine.SetSearchPaths(searchPaths);

        ScriptScope scope = engine.ExecuteFile("C:\Python25\Test.py")
        // Script takes morethan 5mins to execute(sleep in the script)

        ObjectHandle oh = scope.GetVariableHandle("GlobalVariableName")
        // System throws following exception              
        //System.Runtime.Remoting.RemotingException:
        // Object '/011b230e_2f28_4caa_8bbc_92fabb63b311/vhpajnwe48ogwedf6zwikqow_4.rem'

        Console.ReadKey();
    }
  }
}

1 个答案:

答案 0 :(得分:1)

重写InitializeLifetimeServices并返回null将是正常的方法。我怀疑你的情况是否可行。在app.config文件中包含<lifetime> element是另一种方法。