[IronPython] IronPython.Runtime.Types.PythonType Is not marked as Serializable Exception

Dino Viehland dinov at microsoft.com
Mon Apr 12 20:31:29 CEST 2010


Sorry for the delay - I was taking a couple of days off last week and I didn't get a chance to respond before disappearing.

The problem here is that you're trying to return the objects from the remote domain into the local domain.  ObjectOperations actually has a set of overloads which take ObjectHandles and has some other methods which return ObjectHandles for working with objects in a remote app domain.  If I change your modified code to the code below and add a using System.Runtime.Remoting this works  again:

            var subClass = scope.GetVariableHandle("C");                                       // get back a handle
            var instance = ops.Invoke(subClass, new ObjectHandle[0]);            // invoke the handle to create an instance

            mbr = instance.Unwrap() as MbrBase;                                                       // now we know we have an MBR and we can unwrap it to our local side


From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of mohammad mustaq
Sent: Saturday, April 10, 2010 6:51 AM
To: Discussion of IronPython
Subject: Re: [IronPython] IronPython.Runtime.Types.PythonType Is not marked as Serializable Exception

Hi Dino,

Is there any update on this. Should you need more details let me know.

thanks,
Mustaq
On Wed, Apr 7, 2010 at 10:40 AM, mohammad mustaq <mustaq2001 at gmail.com<mailto:mustaq2001 at gmail.com>> wrote:
Hi Dino,

If have tweaked your code to reproduce the exception that I am facing. Let me know if you need more details.

thanks,
Mustaq


using System;
using Microsoft.Scripting;
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;


class Foo
{
    public static void Main(string[] args)
    {
        AppDomain ad = AppDomain.CreateDomain("foo");
        var engine = Python.CreateEngine(ad);
        engine.Runtime.LoadAssembly(
typeof(MbrBase).Assembly);

        var code = engine.CreateScriptSourceFromString(@"
import MbrBase
class C(MbrBase):
    pass

a = C()
", SourceCodeKind.Statements);

        var scope = engine.CreateScope();
        code.Execute(scope);

        Console.WriteLine("Trying to do it... {0}", AppDomain.CurrentDomain.Id<http://appdomain.currentdomain.id/>);
        MbrBase mbr = (MbrBase)scope.GetVariable("a");
//     MY CHANGES

        string isSubClassCode = String.Format("issubclass({0},{1})", "C", "MbrBase");
        ScriptSource script = engine.CreateScriptSourceFromString(isSubClassCode, SourceCodeKind.Expression);
        bool result = (bool)script.Execute(scope);

        if (result == true)
        {
            ObjectOperations ops = engine.Operations;

            object subClass = scope.GetVariable("C");
            object instance = ops.Call(subClass);

            mbr = instance as MbrBase;
        }

//      END OF MY CHANGE

        mbr.DoItVirtually();
        mbr.DoIt();
        Console.ReadKey();

    }
}

public class MbrBase : MarshalByRefObject
{
    public virtual void DoItVirtually()
    {
        Console.WriteLine("Did it virtually {0}", AppDomain.CurrentDomain.Id<http://appdomain.currentdomain.id/>);
    }

    public void DoIt()
    {
        Console.WriteLine("Did it {0}", AppDomain.CurrentDomain.Id<http://appdomain.currentdomain.id/>);
    }
}

On Tue, Mar 30, 2010 at 10:12 PM, Dino Viehland <dinov at microsoft.com<mailto:dinov at microsoft.com>> wrote:
This works for me w/ 2.6.  I've included my simple repro below which creates a new script engine in a remote app domain, loads my assembly in, runs some code which subclasses the MBRO base class, instantiates an instance of this class, and then calls it from a remote app domain.  The key thing here is that when an MBRO is involved a PythonType should not need to be serialized - the type should live in the remote app domain and all execution of that code should also happen in the remote app domain where we have access to the local PythonType object.  Are you also subclassing types which don't derive from MBRO?  It might help to run IronPython w/ -X:ExceptionDetail if the exception is propagating through IronPython - that'll give a better stack trace to understand what's going on.  Or if you can tweak the simple repro below to match the behavior you're seeing that'd be helpful as well.

using System;
using Microsoft.Scripting;
using IronPython.Hosting;

class Foo {
    public static void Main(string[] args) {
        AppDomain ad = AppDomain.CreateDomain("foo");
        var engine = Python.CreateEngine(ad);
        engine.Runtime.LoadAssembly(typeof(MbrBase).Assembly);

        var code = engine.CreateScriptSourceFromString(@"
import MbrBase
class C(MbrBase):
    pass

a = C()
", SourceCodeKind.Statements);

        var scope = engine.CreateScope();
        code.Execute(scope);

        Console.WriteLine("Trying to do it... {0}", AppDomain.CurrentDomain.Id<http://AppDomain.CurrentDomain.Id>);
        MbrBase mbr = (MbrBase)scope.GetVariable("a");
        mbr.DoItVirtually();
        mbr.DoIt();
    }
}

public class MbrBase : MarshalByRefObject {
    public virtual void DoItVirtually() {
        Console.WriteLine("Did it virtually {0}", AppDomain.CurrentDomain.Id<http://AppDomain.CurrentDomain.Id>);
    }

    public void DoIt() {
        Console.WriteLine("Did it {0}", AppDomain.CurrentDomain.Id<http://AppDomain.CurrentDomain.Id>);
    }
}

From: users-bounces at lists.ironpython.com<mailto:users-bounces at lists.ironpython.com> [mailto:users-bounces at lists.ironpython.com<mailto:users-bounces at lists.ironpython.com>] On Behalf Of mohammad mustaq
Sent: Monday, March 29, 2010 8:13 PM
To: users at lists.ironpython.com<mailto:users at lists.ironpython.com>
Subject: [IronPython] IronPython.Runtime.Types.PythonType Is not marked as Serializable Exception

Hi,

I have IronPython embedded in my C# application. I face issues while creating the python engine in a different appdomain.

It is imposed that every class in IronPython inherit the .NET base class say ClassA. ClassA is derived from MarshalByRefObj as I need to pass an instance of this class to a new appdomain.
I create a new appdomain and pass the instance of ClassA to this Appdomain. While calling a method in python class through the instance of ClassA I get an exception mentioning that "Type 'IronPython.Runtime.Types.PythonTyp
e' in Assembly 'IronPython, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' is not marked as serializable".

How do I Serialize this PythonType. One way that i know is to modify the IronPython source and mark the required types as Serializable (but i do not know where it will lead to and its consequences). Could you please suggest a way to perform the required operation. Should you need more details let me know.

thanks,
Mustaq

P.S. I have used both IronPython 2.0 and 2.6.

_______________________________________________
Users mailing list
Users at lists.ironpython.com<mailto:Users at lists.ironpython.com>
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20100412/1e9e3b35/attachment.html>


More information about the Ironpython-users mailing list