A WCF service exposing multiple elements in DataContract as DataMember
[DataMember(IsRequired = true, EmitDefaultValue = false)] public string Source; [DataMember(IsRequired = true, EmitDefaultValue = false)] public string Target;
In generated proxy (through add service reference in VS 2008) at client, the client can pass null or empty string in Source or Target. how can I enforce Source and Target as Required at client side. i.e. client should get an exception if Source or Target is set to null, before invoking the service call.
Well, both null (xsi:nil) and an empty string are values – they just aren’t values you want.
During deserialization (at client or server):
You could try putting some code in the setter to throw an exception for invalid values?
Alternatively (for more complex cases), I believe that data-contracts support deserialization callbacks, which should allow you to validate…
For example, you can add (in a partial class, if necessary, at the client):
For pre-send checks (at the client), you would have to add an extra validation method, or perhaps (again, in a partial class):
However, the server must also validate this – you can’t assume that it is your client code that has sent the message.