-1

I'm working in a .NET Framework 4.x application.

  • Say I pass in a object to child AppDomain using SetData(..) method;
    • the object is MarshallByRef.
  • The code running child AppDomain changes the passed in Data. But the value is not getting reflected when the call returns to the parent domain?
  • Is that by design? Is there a flag/attribute I can set to ensure that data changed is reflected back?

Correction The library was never updated ( to reflect MarhsallByRefObject). The problem fixed itself after using MarshallByRefObject. Performance

Iftikhar Ali
  • 369
  • 3
  • 12
  • 1
    What do you mean by "the object is `MarshallByRef`"? (I assume you mean `MarshalByRefObject`), please post your **actual code** where you call `SetData()` and pass values/objects. – Dai Oct 22 '22 at 00:34
  • Have you read this? https://stackoverflow.com/questions/1767439/passing-data-across-appdomains-with-marshalbyrefobject – Dai Oct 22 '22 at 00:35
  • You do know that AppDomains are gone in .NET Core land, right? Anything you write in .NET 4.x using AppDomains won't survive a port to .NET Core (or 5+). In .NET Framework land, the best way to talk between AppDomains is to use Remoting (also gone in Core-land, I believe). Remoting between two app domains in the same process, using an IPC channel and the Binary Formatter, is _very_ fast. Then again, you'll need to stomache all the caveats about the BinaryFormatter being unsafe – Flydog57 Oct 22 '22 at 03:15
  • @Flydog57 Yes, I'm aware that AppDomains are gone in .Net Core. Here I'm supporting an older application and want to avoid making revolutionary changes. – Iftikhar Ali Oct 23 '22 at 21:02
  • @IftikharAli: yup, just wanted to make sure you knew where you are. I've spent much of my career supporting _old code_ – Flydog57 Oct 23 '22 at 21:09

1 Answers1

0

Correction:

The library was never updated ( to reflect MarhsallByRefObject). The problem fixed itself after using MarshallByRefObject.

Here is my sample class

public class MyObject:MarshallByRefObject{
   public string Value = "Test";
   public ChildObject _ChildObj = null;

   public MyObject(){
        _ChildObj = new ChildObject(){ParentRef = this};
       }
}

[Serializable()]
public class ChildObject
{
  public MyObject ParentRef;

}

public class ProcessToRunInChildDomain
{
 
 public ProcessToRunInChildDomain(){
    MyObject obj = (MyObject)AppDomain.CurrentDomain.GetData("obj");
     obj.Value = "Value Set in Child Domain";
}
}

public class Program
{
public static void TestAppDomain(){
   
    var myObj = new MyObject(){ Value = "Value in Parent Domain"};
    var childDomain = CreateADomain_And_LoadProcessToRunInChildDomain();
     childDomain.SetData("obj", myObj);

     //Note: If myObj is not MarshallByRefObject, the changes made
     //in Process will not reflect in Parent Object and below test fails.
       myObj.Value.Should().Be("Value Set in Child Domain");
      
     //Note: The child field type of my MyObject is just Marked 
   //Serializable but parent reference points to the latest values.
       myObj._ChildObj.ParentRef.Value.Should().Be("Value Set in Child Domain");
  }
}
Iftikhar Ali
  • 369
  • 3
  • 12
  • @Dai My issue was that class didn't have MarshallByRefObject inheritance (Compile Issues). Now it is working. See above code sample. – Iftikhar Ali Oct 23 '22 at 21:33