Hi @Syamala Vishnuvajhala ,
A data contract is a formal agreement between a service and a client that abstractly describes the data to be exchanged.
I suggest you can learn from the documentation.
https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/using-data-contracts
https://learn.microsoft.com/en-us/dotnet/api/system.runtime.serialization.datacontractattribute?view=net-6.0
The following example shows how a data contract for the MyTypes.PurchaseOrder type is created by applying the DataContractAttribute and DataMemberAttribute attributes to the class and its members.
namespace MyTypes
{
[DataContract]
public class PurchaseOrder
{
private int poId_value;
// Apply the DataMemberAttribute to the property.
[DataMember]
public int PurchaseOrderId
{
get { return poId_value; }
set { poId_value = value; }
}
}
}
EDIT
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData();
}
[DataContract]
public class Customer
{
[DataMember]
public int ID { get; set; }
[DataMember]
public string Name { get; set; }
}
public class Ser vice1 : ISe rvice1
{
public string GetData()
{
Customer c = new Customer() { ID = 1, Name = "Allen" };
string json;
using (MemoryStream ms = new MemoryStream())
{
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Customer));
ser.WriteObject(ms, c);
json = Encoding.UTF8.GetString(ms.GetBuffer(), 0, Convert.ToInt16(ms.Length));
}
return json;
}
Best regards,
Lan Huang
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.