|
For some reason Microsoft decided that WCF serialization is not going to support the xsd:choice. The .Net XML serialization does support xsd:choice which is important. Now, if you had a schema that used xsd:Choice and used XSD tool to generate classes for it will generate code similar to:
///<remarks/>
[System.Xml.Serialization.XmlElementAttribute("Option1", typeof(typeOption11), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlElementAttribute("Option2", typeof(typeOption2), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlElementAttribute("Option3", typeof(typeOption3), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public object Item {
get {
return this.itemField;
}
set {
this.itemField = value;
}
Well that is look neat does not it? Now if you exposed this same generated code through WCF, the WSDL would generate item as Object on the client side and the consumer would not get any information from the WSDL about typeOption1, typeOption2, nor typeOption3. That is a real pain for the developer on the consumer side!. So one workaround that I had to device for a solution I was developing is to create another class like the following
public partial class Optionss {
[System.Xml.Serialization.XmlElementAttribute("Option1", typeof(typeOption1), Form=System.Xml.Schema.XmlSchemaForm.Unqualified,IsNullable = true)]
public typeOption1 Option1 = null;
[System.Xml.Serialization.XmlElementAttribute("Optoin2", typeof(typeOption2), Form=System.Xml.Schema.XmlSchemaForm.Unqualified,IsNullable = true)]
public typeOption2 Option2 = null;
[System.Xml.Serialization.XmlElementAttribute("Option3", typeof(typeOption3), Form=System.Xml.Schema.XmlSchemaForm.Unqualified,IsNullable = true)]
public typeOption3 Option3 = null;
}
And chenage the item above to be similar to
public Optionss Item {
get {
return this.itemField;
}
set {
this.itemField = value;
}
Now the options are going to appear to the consumer, however the data you receive is going to be for a different schema so I wrote code that would serialize the received request then “fix” the XML to be corresponding to the actual schema with xsd:choice. Not quite neat workaround but it works.
|