I have a little WCF service that has the following signature:
string ProcessMessage(XmlElement xmlSource);
When I get a request without the xml declaration, the webservice works fine, but as soon as the xml declaration is added, I get a crash.
The request that works
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
<soapenv:Header/>
<soapenv:Body>
<tem:ProcessMessage>
<tem:xmlSource>
<clipped>elements here</clipped>
</tem:xmlSource>
</tem:ProcessMessage>
</soapenv:Body>
</soapenv:Envelope>
The request that doesn't work
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
<soapenv:Header/>
<soapenv:Body>
<tem:ProcessMessage>
<tem:xmlSource>
<?xml version="1.0" encoding="UTF-8"?>
<clipped>elements here</clipped>
</tem:xmlSource>
</tem:ProcessMessage>
</soapenv:Body>
</soapenv:Envelope>
And the crash:
<Message>No characters can appear before the XML declaration. Line 7, position 14.</Message>
<StackTrace>at System.Xml.XmlExceptionHelper.ThrowXmlException(XmlDictionaryReader reader, String res, String arg1, String arg2, String arg3)
at System.Xml.XmlUTF8TextReader.ReadDeclaration()
at System.Xml.XmlUTF8TextReader.Read()
at System.Xml.XmlBaseReader.MoveToContent()
at System.Xml.Serialization.XmlSerializationReader.ReadXmlNode(Boolean wrapped)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderINBUWProcessingService.Read1_ProcessMessage()
at Microsoft.Xml.Serialization.GeneratedAssembly.ArrayOfObjectSerializer.Deserialize(XmlSerializationReader reader)
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)</StackTrace>
<Type>System.Xml.XmlException</Type>
</InnerException>
<Message>There is an error in XML document (7, 14).</Message>
<StackTrace>at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle)
at System.ServiceModel.Dispatcher.XmlSerializerOperationFormatter.DeserializeBody(XmlDictionaryReader reader, MessageVersion version, XmlSerializer serializer, MessagePartDescription returnPart, MessagePartDescriptionCollection bodyParts, Object[] parameters, Boolean isRequest)</StackTrace>
<Type>System.InvalidOperationException</Type>
What I assume is happening is that the WCF service is reading the whole soap envelop as one xml document, then when it encounters the xml declaration (for the parameter xmlSource) and interprets it as the xml document for the whole soap envelope. This is where it crashes and says that the xml declaration must be the first part of the document.
Is there a way to fix this or strip out xml declarations from the request before it hits the webservice?