none
DataContract In WCF

    Question

  • Hi Everyone

    Can anyone explain why we use datacontract in wcf.

    We can do all things without using datacontract. Please explain in deep.


    Ravideep Bansal

    Wednesday, January 09, 2013 6:19 AM

Answers

  • Hi,
    It is not necessery to use DataContract in WCF but you should it as can explicitely control the serialization of types and its members.In addition, you can apply to private fields also which was not allowed in XMLSerializer.
    Please find the below link for more benefits that are provided by using WCF DataContractSerializer :
    http://msdn.microsoft.com/en-us/library/cc656732.aspx

    One good question is equivalent to ten best answers.

    Wednesday, January 09, 2013 7:24 AM
  • The DataContract is just a formal definition of a type that can be understood on both sides of the service boundary.

    A data contract is a formal agreement between a service and a client that abstractly describes the data to be exchanged. That is, to communicate, the client and the service do not have to share the same types, only the same data contracts. A data contract precisely defines, for each parameter or return type, what data is serialized (turned into XML) to be exchanged.

    You're not using DataContracts instead of parameters.... if your parameters are atomic types, like string and int, you don't need data contracts on these.

    But if you create a compound type, which is often beneficial is you have five or more parameters, then you need to decorate that class type with [DataContract] to make it clear to WCF that that's the class you want to use and serialize.

    Having two, three simple parameters is great - but as soon as you have more than that, or if you need to pass back more than a single value, you typically should use a class to group/hold those values together.

    For more details please check the following url

    http://msdn.microsoft.com/en-us/library/ms733127.aspx


    With Thanks and Regards
    Sambath Raj.C
    click "Proposed As Answer by" if this post solves your problem or "Vote As Helpful" if a post has been useful to you
    Happy Programming!

    Wednesday, January 09, 2013 10:22 AM

All replies

  • Hi,
    It is not necessery to use DataContract in WCF but you should it as can explicitely control the serialization of types and its members.In addition, you can apply to private fields also which was not allowed in XMLSerializer.
    Please find the below link for more benefits that are provided by using WCF DataContractSerializer :
    http://msdn.microsoft.com/en-us/library/cc656732.aspx

    One good question is equivalent to ten best answers.

    Wednesday, January 09, 2013 7:24 AM
  • The DataContract is just a formal definition of a type that can be understood on both sides of the service boundary.

    A data contract is a formal agreement between a service and a client that abstractly describes the data to be exchanged. That is, to communicate, the client and the service do not have to share the same types, only the same data contracts. A data contract precisely defines, for each parameter or return type, what data is serialized (turned into XML) to be exchanged.

    You're not using DataContracts instead of parameters.... if your parameters are atomic types, like string and int, you don't need data contracts on these.

    But if you create a compound type, which is often beneficial is you have five or more parameters, then you need to decorate that class type with [DataContract] to make it clear to WCF that that's the class you want to use and serialize.

    Having two, three simple parameters is great - but as soon as you have more than that, or if you need to pass back more than a single value, you typically should use a class to group/hold those values together.

    For more details please check the following url

    http://msdn.microsoft.com/en-us/library/ms733127.aspx


    With Thanks and Regards
    Sambath Raj.C
    click "Proposed As Answer by" if this post solves your problem or "Vote As Helpful" if a post has been useful to you
    Happy Programming!

    Wednesday, January 09, 2013 10:22 AM