How to host an IronPython engine in a separate AppDomain?

故事扮演 提交于 2019-11-29 15:27:02

问题


I have tried the obvious:

var appDomain = AppDomain.CreateDomain("New Domain");
var engine = IronPython.Hosting.Python.CreateEngine(appDomain); // boom!

But I am getting the following error message: Type is not resolved for member 'Microsoft.Scripting.Hosting.ScriptRuntimeSetup,Microsoft.Scripting, Version=0.9.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.

Googling for this error has not proved fruitful sofar...

EDIT #1:

I tried to create a minimal reproducing project by copying the relevant stuff to a new Console Application:

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);
            var scope = engine.CreateScope();
            //scope.SetVariable("revit", _application);
            //engine.Runtime.IO.SetOutput(new ScriptOutputStream(_instance),     Encoding.UTF8);
            //engine.Runtime.IO.SetErrorOutput(new ScriptOutputStream(_instance),     Encoding.UTF8);
            var script = engine.CreateScriptSourceFromString("print 'hello, world!'",     SourceCodeKind.Statements);
            script.Execute(scope);

            Console.ReadKey();
        }
    }
}

This works as expected!

I am thus left to conclude that the error I am getting is related to one of the lines I commented out: The scope added to the engine contains an object I have little control over - a reference to a plugin host this software is intended to run in (Autodesk Revit Architecture 2010).

Maybe trying to pass that is what is creating the error?

Is there a way to pass a proxy instead? (will have to look up .NET remoting...)

EDIT #2:

I have whittled the problem down to passing an object via the scope that does cannot be proxied to the other AppDomain: All objects added to the scope of an IronPython interpreter running in a different AppDomain will have to be marshaled somehow and must thus either extend MarshalByRefObject or be Serializable.


回答1:


Just create your own bootstrapping class that will run in a new AppDomain and will do the initialization of IronPyton there, will it solve the prob?



来源:https://stackoverflow.com/questions/1362757/how-to-host-an-ironpython-engine-in-a-separate-appdomain

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!