locked
DataContract / DataMember annotation not required? RRS feed

  • Question

  • User-1033384211 posted

    Hello all,

    just starting to dig into wcf for the first time and it looks like it could be a lot of fun.  got myself a intro book, and its going well.  Im curious however about the necessity for annotating complex types with datacontact / datamember.  my book says its required if you want your own complex types passed to the client.  thats my understanding at least.  however, my client projects can always see and get my data types regardless of whether or not i use [datacontract] and [datamember].  couldnt find much on this through my searches other than you dont need it if your service is being consumed by .net client but it is required for other client types like java?  and thats where the wsdl definition comes to play, but even without the annotations i can see info on my classes in the wsdl.  for example, my very simple interface and class:

        [ServiceContract]
        public interface ITestPlatform
        {
            [OperationContract]
            Employee getEmployee();
        }
    
        
        public class Employee
        {
            private string _first;
            private string _last;
    
            public string first
            {
                get { return _first; }
                set { _first = value; }
            }
    
            public string last
            {
                get { return _last; }
                set { _last = value; }
            }
        }
    

    no annotations on my employee class, yet in my wsdl i see this small section:

    <xs:complexType name="Employee">
       <xs:sequence>
          <xs:element name="first" type="xs:string" nillable="true" minOccurs="0"/>
          <xs:element name="last" type="xs:string" nillable="true" minOccurs="0"/>
       </xs:sequence>
    </xs:complexType>

    since this seems pretty fundamental to wcf, i was hoping someone could please explain what im obviously not understanding before i continue my wcf education...

    Thanks

     

     

    Sunday, January 13, 2013 9:19 PM

Answers

  • User-1662538993 posted

    The DataContract is just a formal definition of a type that can be understood on both sides of the service boundary.

    If you return, as in your example, a "MyUserCollection" object, the consumers of your service will need to reference the innards of your service/system, which is a violation of the SOA tenet of explicit boundaries. By using a DataContract, you are publishing the structure of your return types in a loosely-coupled way.

    Check this link-

    http://stackoverflow.com/questions/303013/whats-the-point-of-a-datacontract-in-wcf

    http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/0ce52b67-0402-4bdf-acaa-fa74be111b57

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 15, 2013 3:25 PM