none
cross domain objects life time RRS feed

  • Question

  • Hi! So, I'm doing cross-domain calls using AppDomain.DoCallBack, in the class marked as Serializable (passed by value, right?).

    I would like to ask, if it is passed by value, does it mean the constructor is called in the other domain again? Is the destructor called when the domain is unloaded?

    Thanks, Jan
    • Edited by Jan Kučera Friday, August 14, 2009 12:56 PM
    Friday, August 14, 2009 12:55 PM

Answers

  • Yes, the deserializer will call the constructor to create the new object.  Yes, a finalizer is invoked when the object is collected.

    Hans Passant.
    • Marked as answer by Jan Kučera Friday, August 14, 2009 2:15 PM
    Friday, August 14, 2009 2:12 PM
    Moderator

All replies

  • Yes, the deserializer will call the constructor to create the new object.  Yes, a finalizer is invoked when the object is collected.

    Hans Passant.
    • Marked as answer by Jan Kučera Friday, August 14, 2009 2:15 PM
    Friday, August 14, 2009 2:12 PM
    Moderator
  • So... it is not a good idea to create an AppDomain in such constructor, right?

    And who should take care of the AppDomain? Is it wrong idea to unload it during disposal?
    eg.

    [Serializable]
    public class Test : IDisposable
    {
      [NonSerialized] createdDomain;

      public Test()
      { 
          createdDomain = AppDomain.Create("Test");
      }

      public void Dispose()
      {
         if (createdDomain != null)
             AppDomain.Unload(createdDomain);
      }

      // ---------------------------------------------------

      public void DoWork()
      {
        createDomain.DoCallBack(DoWorkInternal);
      }
      private void DoWorkInternal()
      {
        ...
      }

    }
    Friday, August 14, 2009 2:18 PM
  • And second question, if the class is passed back from the created domain to the default one, is the constructor called again? Or if there is any literature I could learn from about this topic, I would be glad to know!
    Saturday, August 15, 2009 8:41 PM
  • Objects can only cross the AppDomain through serialization.  So, yes.  What is your hang-up with this?

    Hans Passant.
    Saturday, August 15, 2009 9:00 PM
    Moderator
  • It is a bit diffult for me to figure out how many instances of the class are instantiated and which are which, and which gets discarded when. Moreover there is no easy way to see the list of created AppDomains...

    This means that using the constructor I wrote above:
    public Test()
      { 
          createdDomain = AppDomain.Create("Test");
      }

    a new domain will be created at every domain boundary crossing, which is probably not a good idea. So I ended with
    public Test()
      { 
          if (createdDomain == null && AppDomain.CurrentDomain.FriendlyName != "Test")
                    createdDomain = AppDomain.Create("Test");
      }

    I'm just looking what's up all around and to avoid mistakes, it's my first experience with multiple domains...
    Saturday, August 15, 2009 9:56 PM