7

How can you make WCF use xs:All instead of xs:Sequence when it defines complex object types in the wsdl/xsd for a web service?

The issue I am having is that xs:Sequence requires that calling applications pass the elements in the soap message in the order specified in the WCF generated xsd (this is alphabetical by default). xs:All (or choice for that matter) does not care about the order.

Can this behaviour be changed simply through a configuration option somewhere?

Dean Johnston
  • 446
  • 4
  • 7
  • You could write your own extension to create the WSDL if you really want to - it's not totally trivial, but it can be done. Search the web for "WCF WSDL extension" - there should be quite a few samples out there – marc_s Sep 30 '09 at 08:44
  • Does this answer your question? [XML validation with XSD: how to avoid caring about the sequence of the elements?](https://stackoverflow.com/questions/3325247/xml-validation-with-xsd-how-to-avoid-caring-about-the-sequence-of-the-elements) – Michael Freidgeim Jan 19 '21 at 01:09

3 Answers3

3

From the top of my head, I think you can't. What you can do instead, is to write the WSDL file by hand, then use svcutil.exe to generate the code.

If all you want to do is order elements in a different order than alphabetically, you can order the elements in the DataContract, using the Order (starting at 1, not 0 like arrays) parameter on the [DataMember] attribute ([DataMember(Order = 1)], [DataMember(Order = 2)], etc).

Philippe
  • 3,945
  • 3
  • 38
  • 56
2

You can switch WCF to use the XmlSerializer instead of DataContractSerializer. The XmlSerializer supports xs:all. See http://msdn.microsoft.com/en-us/library/ms733901.aspx

Eugene Osovetsky
  • 6,443
  • 2
  • 38
  • 59
0

Even if you could force WCF to do so, the deserializer would not work correctly to support the input. Examples and explanation below.

Input 1 (good):

<MyOperation>
  <AField>value A</AField>
  <BField>value B</BField>
</MyOperation>

Input 2 (bad):

<MyOperation>
  <BField>value B</BField>
  <AField>value A</AField>
</MyOperation>

So if input 1 deserialized correctly, then input 2 would not -- BField would have a value that was set, but the property AField would be null.

If WCF cannot handle this out-of-sequence input, I strongly think that it should throw an exception, but based on my testing (.NET 3.5 in IIS) it does not do so, it just skips some element values.

Furthermore, WCF also ignores totally bogus input, as long as it does not affect the valid element values it is looking for. So this input

<MyOperation>
  <bogusField>with or without data</bogusField>
  <AField>value A</AField>
  <bogusField2 />
  <BField>value B</BField>
  <bogusField3></bogusField3>
</MyOperation>

would not throw any errors, and would actually deserialize the values in AField and BField.

Abacus
  • 2,041
  • 1
  • 18
  • 23