Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is DataContract attributes required for WCF

I'm writing WCF service and his client. I want to send/receive objects of my custom classes, between service and client.

I have 3 modules

  • WCF service
  • WCF client
  • common class library

Both WCF service and client have references to common class library. I don't want to mark all my class with DataContract attributes.

So, my question "Is DataContract attributes required for WCF?"

I use .NET 4 and netTcpBinding.

like image 312
user179437 Avatar asked May 07 '11 14:05

user179437


People also ask

Why DataContract is used in WCF?

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.

Is DataMember attribute required?

DataMember attribute is not mandatory to add to serialize data. When DataMember attribute is not added, old XMLSerializer serializes the data. Adding a DataMember provides useful properties like order, name, isrequired which cannot be used otherwise.

What is DataContract and DataMember in WCF?

A datacontract is a formal agreement between a client and service that abstractly describes the data to be exchanged. In WCF, the most common way of serialization is to make the type with the datacontract attribute and each member as datamember.

What is DataContract attribute?

[DataContract] attribute specifies the data, which is to serialize (in short conversion of structured data into some format like Binary, XML etc.) and deserialize(opposite of serialization) in order to exchange between the client and the Service.


1 Answers

If I recall correctly(IIRC), if you don't use formal data-contract markers, it defaults to acting like a field-serializer. This will work, but is less easy to version, since private changes can break the client/server. IMO you should always formally decorate WCF types with the data-contract/data-member attributes. It will work without them, but for the wrong reasons (IIRC, originally it didn't work without formal markers).

like image 53
Marc Gravell Avatar answered Sep 23 '22 11:09

Marc Gravell