Answered Default Constructors and Proxy Classes

  • 8. května 2006 22:22
     
     

    Hi,

    I have a question regarding default constructors and the way svcutil generates proxy. I‘m not sure if I’m thinking in the right way in exposing these classes as DTO’s. I greatly value your inputs,

     

    With the same example given below

    [DataContract]

        public class MyClass

        {

            private List<Child> _children;

               

            void MyClass

    {

    _children = new List<Child>();

    }

    [DataMember]

    Public List<Child> Children

    {

    Get{return _children;}

    set{_children = value;}

     

    }

        }

     

    I’m trying to expose this class via a service agent. And for that I’m using svcutil to generate the DTO Classes. How can I ensure that the client when it tries to do the following does not get a null value?

    MyClass myInstance = new MyClass();

    myInstance.Children.Add(new Child()); //Throws an exception as the Children is null

     

    Am I doing the right thing in exposing these proxy class as DTO I greatly appreciate your response.

     

    Thanks much,

    Kumar

Všechny reakce

  • 9. května 2006 16:53
     
     Odpovědět

    The constructor will be called if you create the object normally. However, it will not run if the object is deserialized off the wire.  If you need to have a default value in place during serialization/deserialization, you call use the Version Tolerant Attributes to do that.

    Please see this for further information:

    http://msdn2.microsoft.com/en-US/library/ms229752.aspx

    Hope that helps.

    Thanks!

    Scott

  • 3. prosince 2006 16:06
     
     

    I have the same requirement.

    But from the reference link, I do not know in which way VTA will help.

    Can anyone explain it more clearly?

     

    Thanks a lot.

  • 3. prosince 2006 16:12
     
     

    I read it again.

    Maybe OnDeserializing Attribute can be used to solve it.

    I will try it later.

    Thanks again.