DotnetDude
DotnetDude

Reputation: 11807

Best practice for Service and data contracts - WCF

I understand I can apply several options to the ServiceContract (like Name, Namespace) attribute and for OperationContract (Action, ReplyAction)

The same goes to DataContract (Namespace) and DataMember (IsRequired, Name, Order)

How do I determine if I need to apply a particular option or not. What is the best practice/convention I should follow?

Upvotes: 6

Views: 2486

Answers (2)

Aaronaught
Aaronaught

Reputation: 122624

There's no one "best practice" here. Just understand what all of the different arguments are used for.

  • Name should be specified if you want the "public" name of your service to be different from the actual class name (most people don't change this). It's similar for data contracts - use it if you want the name exposed over SOAP/MEX to be different from the property name you use internally.

  • Namespace is something you should change, otherwise it defaults to tempuri.org - you should replace this with a namespace that's relevant to your application.

  • IsRequired should be specified if the type is nullable (i.e. a string) but the field is actually required as part of the contract (for example, a customer must have a name... that is a required field).

  • Order just changes the order that properties appear in the metadata/XML; usually most people don't bother with this unless it's required for compatibility reasons.

Upvotes: 8

Sky Sanders
Sky Sanders

Reputation: 37074

There are not requirements or standards.

The attributes provide options, increasing the possibility that the static spec provided by MS will fit your needs.

So, I would say best practice is to understand the options and how to apply them to your requirements.

Upvotes: 0

Related Questions