how do we create a data contract

Syamala Vishnuvajhala 41 Reputation points
2022-07-19T04:39:26.337+00:00

how do we create a sample data contract

Developer technologies .NET Other
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Lan Huang-MSFT 30,186 Reputation points Microsoft External Staff
    2022-07-19T07:58:45.197+00:00

    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;  
        }  
    

    222488-image.png
    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.

    0 comments No comments

  2. Syamala Vishnuvajhala 41 Reputation points
    2022-07-19T09:49:52.663+00:00

    Thanks Lan, the one which is in example is in C#.
    can you provide any sample code in YAML or json please


  3. Syamala Vishnuvajhala 41 Reputation points
    2022-07-20T03:39:25.037+00:00

    can we do a data contract in YAML/json instead of C# ? The one which is there in the docs are in C#


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.