0

I'm in the process of implementing a WCF/Soap service operation and have defined several of the data contract's members as not required (IsRequired = false).

My question relates to the following two scenarios...

  • The client supplies a null value for the contract element (i:nil="true")
  • The client does not supply the optional element (i.e. they send me a partial contract)

This problem is significant for both complex and nullable primitive types. From what I can determine, the .Net serializer returns null in both scenarios, so my question is, can anyone recommend a way to determine whether an optional parameter was supplied in the SOAP request?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Jonna
  • 1
  • 2

1 Answers1

0

One workaround would be to add boolean MemberSupplied type members in your data contract. E.g.:

[DataContract]
public class EmployeeSearchParams
{
    [DataMember]
    public Employee Manager {get; set;}

    [DataMember]
    public bool ManagerSupplied {get; set;}
}
Eren Ersönmez
  • 38,383
  • 7
  • 71
  • 92
  • Thanks for the workaround, I've actually seen this suggested on another thread relating to a different issue. I could use this approach if necessary, but I'd rather avoid it if possible as it's somewhat clunky. I'm reluctant to change my contract to overcome the issue if I can avoid it. Talking to a colleague yesterday, Java has a cleaner solution to the problem whereby the serializer provides the de-serialized object along with a wrapper for the raw request which provides properties to determine whether an optional element was supplied. – Jonna Jan 04 '13 at 08:48
  • @Jonna that was my initial feeling about this approach as well, but although I call this a _workaround_, I came to accept this as the _cleaner_ solution. In my opinion it gets much more complicated to deal with the raw SOAP on the server side. Also consider the client side. If it's a straight forward WCF client, the client will have no way to choose whether to send null or omit the member on a _call-by-call_ basis. – Eren Ersönmez Jan 04 '13 at 10:45