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

C# 如何在单独的AppDomain中托管IronPython引擎?,c#,ironpython,embedding,C#,Ironpython,Embedding,我使用下面的代码在与c#分开的“appDomain”上执行IronPython脚本。 (我使用这种方法来解决内存泄漏问题) 花费较少时间(少于3分钟)的脚本执行良好 但是如果脚本需要更长的时间(超过5分钟)抛出异常 ->System.Runtime.Remoting.RemotingException:对象“/011b230e_2f28_4caa_8bbc_92fabb63b311/vhpajnwe48ogwedf6zwikqow_4.rem” using System; using Micro

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

但是如果脚本需要更长的时间(超过5分钟)抛出异常 ->System.Runtime.Remoting.RemotingException:对象“/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();
    }
  }
}

重写InitializeLifetimeServices并返回null将是正常的方法。我怀疑这在你的情况下是可能的。在app.config文件中包含是另一种方法。

我没有真正的解决方案,但作为一个快速解决方案,我相信您可以通过设置LifetimeServices.LeaseTime来增加AppDomain的远程处理超时。我认为必须在目标AppDomain中执行(但尚未测试!)。最简单的方法可能是让沙盒域中的Python引擎为您设置属性。InitializeLifetimeService的覆盖似乎在2.0.x中缺失,但在IronPython 2.6中存在。