Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1005271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:16:05+00:00 2026-05-16T08:16:05+00:00

I am having issues while passing objects across Appdomains.During further investigation I found that

  • 0

I am having issues while passing objects across Appdomains.During further investigation I found that the issue is due to the IronPython object not been Serialized. This IronPython object is derived from a .NET base class. The .NET base class is derived from MarshalByRefObj.

Let me explain my environment. I have IronPython embedded in my C# application. 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 another 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.PythonType’ in Assembly ‘IronPython, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35’ is not marked as serializable”

How can I serialize python objects from C# or is there any other way to tackle this situation.

Update

More insight into the problem. If I instantiate the class where I access python methods in the default appdomain and then pass the instance to the created appdomain then the above mentioned issue is not seen. On the other side when I Instantiate the class where I access the python method in the created appdomain and then access the python methods then the serialization exception is thrown.

One way I see to resolve this issue is that i modify the IronPython source code to serialize the types that are required. Is there any other way that i can do to get around this issue.

Here is a sample code to reproduce the exception I encountered

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);
        MbrBase mbr = (MbrBase)scope.GetVariable("a");

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

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

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);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T08:16:06+00:00Added an answer on May 16, 2026 at 8:16 am

    This works for me now.

    The problem was that I was trying to return the objects from the remote domain into the local domain. ObjectOperations 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 the above code is modified to the code below it works.

    Add: using System.Runtime.Remoting

            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
    

    P.S. I got the solution from Iron Python community.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having issues passing arguments through run to the windows side To demonstrate, it
I have a huge web app that is having issues with memory leak in
I'm having issues with my SQL Reporting Services reports. I'm using a custom font
I'm having issues creating an ActionLink using Preview 5. All the docs I can
I'm having issues with color matching css background colors with colors in images on
I am having issues converting a png to tiff. The conversion goes fine, but
I'm having issues getting Firefox to update a webpage when its class is changed
At work we're having issues with different people wanting/suggesting different names for a new
I m new to use ankhSVN and having issues. I have created some new
I have Visual Studio 2008 Professional and I am having issues with expanding and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.