views:

120

answers:

2

lHi,

I'm currently writing a WCF service. One of those methods get's a request object and returns a response object. In the request there are a couple of value-type members.

Is there a way to define members are mandatory in the declarative way? I'm in an early stage of development and I don't want to start with versioning now. In addition I don't want to have method sig with 25 parameters, therefore I created the request object.

The problem I have is that due to the value-types, I can never be sure if the consumer of the service intended to have the default value in there, or it was just by lazyness. On consumer side you don't easily detect that you probably missed that property.

So I would like to have something that forces the caller of the service to provide an value, and if not he ideally get's a compile-time error.

any ideas?

tia, Martin

A: 

Yes, absolutely:

[DataContract]
public class YourRequestClass
{
   [DataMember(IsRequired="True")]
   int RequestID { get; set; }

}

There are a number of sub-attributes to the DataMember attribute that you can use - Order and IsRequired probably being the most frequently used ones.

marc_s
Hi, thx for yor answer, but this doesn't really solve my problem. Because as soon as I update the my service reference on the client, nobody will bother me to really provide a value there. The serialization will add this to the request, and the service accepts it, even if it's just the default.
Martin Moser
+1  A: 

Please check if the following is resolving your issue:

IsRequired/EmitDefaultValue Attribute on DataMember

http://social.msdn.microsoft.com/forums/en-US/wcf/thread/d9e45449-cc50-42e2-b955-75ab86f01d4f

The topic above describes a combination of IsRequired and EmitDefaultValue attributes set on a request member, which according to the discussion there at least seams to resolve the "issue"

cheers

pointernil