user179437
user179437

Reputation: 713

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

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.

Upvotes: 30

Views: 18835

Answers (4)

Marc Gravell
Marc Gravell

Reputation: 1064104

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).

Upvotes: 23

Juan Gomez
Juan Gomez

Reputation: 1518

It's not required to use DataContract attributes, your service will work just fine without them.

Having said that, I really don't think having your service and client share a class library is the best design approach. It would be a lot clearer if you'd expose your types through DataContracts.

What would happen when someday, someone wants to consume your service but don't have access to the shared class library?

Upvotes: 3

Jack Ukleja
Jack Ukleja

Reputation: 13521

"Is DataContract attributes required for WCF"

Technically, no. This is dependent on whether you use the DataContractSerializer or not (which is the default on many bindings).

There are other options:

  1. Consider alternative serializers
  2. Try using Data Contract Surrogates (attributes are still required somewhere, but it does potentially mean u can leave your class untouched, if thats relevant)
  3. Rely on the serialization defaults for when you serialize classes without Data Contract attributes

Upvotes: 7

Matthew Abbott
Matthew Abbott

Reputation: 61617

You can use several serializaton techniques with WCF, it's one of the nice adaptable things about it. Have a look:

http://msdn.microsoft.com/en-us/magazine/cc163569.aspx

Upvotes: 3

Related Questions