tags:

views:

21

answers:

2

I have a DTO type declared as follows:

[Serializable]
public class PersonDTO
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

I have a WCF service whose operation contract reads as follows:

[OperationContract]
public PersonDTO GetPerson(int id);

The problem that I have is when I consume this service by using 'Add Service Reference', the wsdl contains as follows:

<xs:schema elementFormDefault="qualified" targetNamespace="http://schemas.datacontract.org/2004/07/Test.DTO"&gt;
−
   <xs:complexType name="PersonDTO">
−
      <xs:sequence>
          <xs:element name="_x003C_Id_x003E_k__BackingField" nillable="true" type="xs:int"/>
          <xs:element name="_x003C_FirstName_x003E_k__BackingField" nillable="true" type="xs:string"/>
          <xs:element name="_x003C_LastName_x003E_k__BackingField" nillable="true" type="xs:string"/>
      </xs:sequence>
   </xs:complexType>
   <xs:element name="IVQueueDTO" nillable="true" type="tns:IVQueueDTO"/>
</xs:schema>

When I try to reference on the WCF client, instead of getting person.Id and person.FirstName, I'm getting person.Idk__BackingField, person.FirstNamek_BackingField, etc.

What should I do to get the exact type as I defined on the WCF service side? I'm using Serializable attribute on PersonDTO, because, this service needs to be interoperable with java. I'm using .NET Framework 4.0, C#, Visual Studio 2010, Win XP SP3. The WCF service exposes http endpoint and uses basicHttpBinding.

+2  A: 

Have you tried using DataContact and DataMember attributes instead of Serializable? I believe that will still give you a serializable object that Java can access.

Jerod Houghtelling
Thanks. DataContract did work.
Matrix
Yes, the key to this is that the DataContract and DataMember attributes allow you to specify the name and other metadata. See http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractattribute.aspx
Rob Cooke
@matrix please mark the answer as accepted if this solution woked (the checkbox under the up/down voting). Thank You!
Jerod Houghtelling
A: 

DataContact fixes this issue. But I'm not sure whether DataContract is interoperable with Java. I'm yet to test this. But I'm pretty sure that the issue described above is due to the Serializable attribute. Check out this link. This makes me to believe that DataContract should be interoperable.

WCF Data Contracts and "k__BackingField" Property Naming

Matrix