Reputation: 168
I'm having a strange case. I'm using the contract-first approach to develop a WCF Web Service. The thing is I have defined a specific attribute order in the XSD file, with the tag. Nevertheless the Generated code with the contract-first tool is messing this order, and when I try to send a xml with the specified attribute distribution I'm getting this error (using the SoapUI application):
Element' 'codigoProvinciaField' from namespace 'http://schemas.datacontract.org/2004/07/InventarioWS.ContractTypes' is not expected. Expecting element 'codigoCorporacionField' (translated from the message below)
It works when I swap the "codigoCorporacionField" with the "codigoProvinciaField". The thing is I've them defined the other way around in the xml schema.
It seems that without the contract-first approach I can use the [DataMember(Order = 0)] tag.
The message:
'http://schemas.datacontract.org/2004/07/InventarioWS.ContractTypes'. Se espera el elemento 'codigoCorporacionField'. Error en la línea 9, posición 42. No se esperaban los elementos 'Element' 'codigoProvinciaField' del espacio de nombres 'http://schemas.datacontract.org/2004/07/InventarioWS.ContractTypes'. Se espera el elemento 'codigoCorporacionField'. en System.Runtime.Serialization.XmlObjectSerializerReadContext.ThrowRequiredMemberMissingException(XmlReaderDelegator xmlReader, Int32 memberIndex, Int32 requiredIndex, XmlDictionaryString[] memberNames) en System.Runtime.Serialization.XmlObjectSerializerReadContext.GetMemberIndexWithRequiredMembers(XmlReaderDelegator xmlReader, XmlDictionaryString[] memberNames, XmlDictionaryString[] memberNamespaces, Int32 memberIndex, Int32 requiredIndex, ExtensionDataObject extensionData) en ReadCodigoEnteFromXml(XmlReaderDelegator , XmlObjectSerializerReadContext , XmlDictionaryString[] , XmlDictionaryString[] ) en System.Runtime.Serialization.ClassDataContract.ReadXmlValue(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context) en System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator reader, String name, String ns, Type declaredType, DataContract& dataContract) en System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator xmlReader, Int32 id, RuntimeTypeHandle declaredTypeHandle, String name, String ns) en ReadEnvioFromXml(XmlReaderDelegator , XmlObjectSerializerReadContext , XmlDictionaryString[] , XmlDictionaryString[] ) en System.Runtime.Serialization.ClassDataContract.ReadXmlValue(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context) en System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator reader, String name, String ns, Type declaredType, DataContract& dataContract) en System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator xmlReader, Type declaredType, DataContract dataContract, String name, String ns) en System.Runtime.Serialization.DataContractSerializer.InternalReadObject(XmlReaderDelegator xmlReader, Boolean verifyObjectName, DataContractResolver dataContractResolver) en System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator reader, Boolean verifyObjectName, DataContractResolver dataContractResolver) en System.Runtime.Serialization.DataContractSerializer.ReadObject(XmlDictionaryReader reader, Boolean verifyObjectName) en System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.PartInfo.ReadObject(XmlDictionaryReader reader, XmlObjectSerializer serializer) en System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.DeserializeParameterPart(XmlDictionaryReader reader, PartInfo part, Boolean isRequest) System.Runtime.Serialization.SerializationException El formateador inició una excepción al intentar deserializar el mensaje: Error al intentar deserializar el parámetro http://tempuri.org/:envio. El mensaje de InnerException era 'Error en la línea 9, posición 42. No se esperaban los elementos 'Element' 'codigoProvinciaField' del espacio de nombres 'http://schemas.datacontract.org/2004/07/InventarioWS.ContractTypes'. Se espera el elemento 'codigoCorporacionField'.'. Consulte InnerException para obtener más información.
The XSD file:
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="Envio">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Version" type="xsd:string">
</xsd:element>
<xsd:element ref="CodigoEnte" minOccurs="1" maxOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="CodigoEnte">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="CodigoComunidad">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:length value="2"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="CodigoProvincia">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:length value="2"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="CodigoCorporacion">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:length value="3"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="Tiporg1">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:length value="1"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="Tiporg2">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:length value="1"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="Tiporg3">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:length value="3"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
And the XML sent using SoapUI:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/" xmlns:inv="http://schemas.datacontract.org/2004/07/InventarioWS.ContractTypes">
<soapenv:Header/>
<soapenv:Body>
<tem:RecuperaCorporacionXCodigo>
<!--Optional:-->
<tem:envio>
<inv:codigoEnteField>
<inv:codigoComunidadField>12</inv:codigoComunidadField>
<inv:codigoProvinciaField>28</inv:codigoProvinciaField>
<inv:codigoCorporacionField>022</inv:codigoCorporacionField>
<inv:tiporg1Field>A</inv:tiporg1Field>
<inv:tiporg2Field>A</inv:tiporg2Field>
<inv:tiporg3Field>000</inv:tiporg3Field>
</inv:codigoEnteField>
<inv:versionField>?</inv:versionField>
</tem:envio>
</tem:RecuperaCorporacionXCodigo>
</soapenv:Body>
</soapenv:Envelope>
Edit: I forgot to say the error comes from an unsolicicited alfabetical order given by the framework/tool. In this answer says:
By specifying the Order property in this way, RegionID will come before RegionDescription in the generated schema. Without this property, the sequence in the schema is sorted alphabetically. Visual Studio will generate the correct proxy, but I don't know how your own class looks like. Make sure your client side specify the same Order property.
Upvotes: 0
Views: 281
Reputation: 168
I've made a workaround, by renaming the troublesome attributes with a "param1..." prefix. I write this for future references, but should have a REAL fix.
Upvotes: 0
Reputation: 5027
I can't explain why the problems manifests itself, but I do think there is a workaround.
Replace <xsd:sequence>
in your XSD with <xs:all>
. The <all>
indicator specifies that the child elements can appear in any order, and that each child element must occur only once. This should fix your problem.
Upvotes: 1