Passing args to a new AppDomain RRS feed

  • Question

  • Hi

    I am devising a server that will have an "isolation" option, this option will allow a client/server session (this is a state based session where client and server side's reatin state data of conversations) to be run purely within a private AppDomain, any bugs etc that cause serious crashes during the session will not bring down the server.

    In order to do this I'd like to pass a Socket object to the new AppDomain when I create it, this Socket will have been returned from an earlier Accept operation from the main server thread.

    Is this possible? can I simply pass refernces from local objects to new AppDomains? if so it begs the question: how can the new and original AppDomains be considered as isolated when they share a reference to a shared object instance?

    Any info much appreciated.



    Thursday, January 13, 2011 5:27 PM


  • You can't pass most objects, by reference, across an AppDomain boundary.


    In order to do this, there are two options - first, the object can be [Serializable].  This will actually use serialization to copy the object into the new appdomain, so you get a copy of your object.


    For smaller objects representing data, this is a great option.


    The other option is to derive a class from MarshalByRefObject.  This works differently - instead of passing the object, a reference to the object is passed, and all method calls are marshalled across the AppDomain boundary via serialization.  The actual method runs in the original appdomain, working with a copy of the arguments, and the result is serialized back across the boundary again.



    Reed Copsey, Jr. -
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by Cookie Luo Thursday, January 20, 2011 8:57 AM
    Thursday, January 13, 2011 6:24 PM