سؤال

Recently I came across a weird behavior in WCF serialization.

Its a simple service which exposes one operation contract and returns object of a class (This class is not decorated with [Serializable] or [DataContract] attributes.

However when I call this operation from client side It works perfectly and consumer receives a object of that class (with actual data in it).

As far as I believe it should not be passed over wire as it cannot be serialized ,Please let me know if I am missing something.

Code Sample

 public class MyService:IService
{      

    public Person GetPerson()
    {
        Person person = new Person();
        person.Name = "Brian";
        return person;
    }


}



public class Person 
{

    public string Name { get; set; }      

}

<services>

  <service name="MyService">
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:8000/MyService"/>
      </baseAddresses>
    </host>

    <endpoint address="TestService1"
              binding="basicHttpBinding"
              contract="BindingTestServer.IService" ></endpoint>

  </service>
</services>
   <behaviors>
  <serviceBehaviors>
    <behavior>
      <serviceMetadata httpGetEnabled="True"/>
      <serviceDebug includeExceptionDetailInFaults="False" />
    </behavior>
  </serviceBehaviors>
</behaviors>

هل كانت مفيدة؟

المحلول

Since a lot of programmers were overwhelmed with the [DataContract] and [DataMember] attributes, Microsoft made the data contract serializer handle all classes - even without any of those attributes - much like the old XML serializer.

So you don't have to add data contract or data member attributes anymore - if you don't then the data contract serializer will serialize all public properties on your class, just like the XML serializer would.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top