views:

307

answers:

2

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?

A: 

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.

Sky Sanders
+1  A: 

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.

Aaronaught
@Aaronaught - Does Namespace have any relation to the URL the services will be hosted in? I know I can specify any namespace I want, but was not sure if it's a good practice to have a namespace same as where the web host is located.
DotnetDude
@DotnetDude: No, not really. Namespace is just supposed to be a unique identifier that differentiates your `AwesomeService` from everyone else's `AwesomeService`. If you happen to have a real production URL, it won't *hurt* you to use that (I do), but it's not a requirement.
Aaronaught
Namespace and Name affect the shape of the generated xml and ultimately the deserialized objects on the other end of the wire.
Sky Sanders
Name is useful for exposing overloaded methods in WCF.
DaveB