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

Dino Viehland dinov at microsoft.com
Tue Mar 30 18:42:27 CEST 2010

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);

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

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

        var scope = engine.CreateScope();

        Console.WriteLine("Trying to do it... {0}", AppDomain.CurrentDomain.Id);
        MbrBase mbr = (MbrBase)scope.GetVariable("a");

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

    public void DoIt() {
        Console.WriteLine("Did it {0}", AppDomain.CurrentDomain.Id);

From: 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
Subject: [IronPython] IronPython.Runtime.Types.PythonType Is not marked as Serializable Exception


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=, 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.


P.S. I have used both IronPython 2.0 and 2.6.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20100330/31335ab0/attachment.html>

More information about the Ironpython-users mailing list