0

I noticed using SoapUI that the DataContract (request) parameter of my OperationContract is considered "optional". (I also tested it and in fact i can avoid to pass the request at my wcf method, or pass it as null)

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:con="..." xmlns:data="...">
   <soapenv:Header/>
   <soapenv:Body>
      <con:MyMethod>
         <!--Optional:-->
         <con:request>
            <data:Id>?</data:Id>
         </con:request>
      </con:MyMethod>
   </soapenv:Body>
</soapenv:Envelope>

On a DataMember, like Id in the example, i can put the attributes

[DataMember(IsRequired = true, EmitDefaultValue = false)]
public string Id {get; set;}

In this case if I don't put at all the xml Id part, or I put it has null I get a serializzation exception as response, and my OperatationContract method is never called, and therefore i can be sure in the implementation of my method that Id will be != null.

I don't know how to do this with the request itself though. The easy solution would be check in every Operation method if the request is null and in that case return an error message. I hope that there is a more versatile solution though

thestral
  • 971
  • 1
  • 6
  • 12
  • Possible duplicate of [How can I force WCF to autogenerate WSDLs with required method parameters (minoccurs="1")?](http://stackoverflow.com/questions/1438623/how-can-i-force-wcf-to-autogenerate-wsdls-with-required-method-parameters-minoc) – Albireo Nov 26 '15 at 10:38
  • @Albireo looks similar, he was looking just to make it required though, and not also having the EmitDefualtValue false behaviour – thestral Nov 26 '15 at 10:43
  • If your contract requires a field and this field is not present in your xml a serialization/deserialization error will happen. What exactly do want, to inspect the xml prior to deserialization? – Ricardo Pontual Nov 26 '15 at 10:48
  • @Sophie as you stated your question your problem is you're receiving a `null` value for a non-nullable value type field and WCF is not able to map the request to the contract. If you make the field mandatory *and* it's a value type, you'll never receive a null and you'll no longer need `EmitDefaultValue` as non-nullable value types does not allow for `null`s (unless you're using the `EmitDefaultValue` to prevent something else — in your example to avoid `Id` to be `0`, the default value for `int`). – Albireo Nov 26 '15 at 10:55
  • The problem is not on the Id, but on the – thestral Nov 26 '15 at 12:04
  • The problem is not on the Id, but on the The request is an object with the attribute DataContract, I don't want the request to be not present or null – thestral Nov 26 '15 at 12:17
  • You must modify the dataContract attribute for request class instead. – Ricardo Pontual Nov 26 '15 at 13:30
  • @RicardoPontual what you mean with modify dataContract? – thestral Nov 26 '15 at 13:39

0 Answers0