0

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?

3
  • What are you using as the client for the WCF service? Are you building your own requests from scratch or using a Web Reference or a WCF ChannelFactory> Commented Aug 19, 2013 at 22:06
  • The requests are coming in from a JAVA app that I unfortunately have no control over. Commented Aug 20, 2013 at 14:21
  • If you really can't get the client to send valid XML then I think you might have to create a non-WCF handler which accepts the content, cleans it and then forwards it to the WCF service. I had a look at the various interception points as described in this document but they all seem to be after the request has been parsed as SOAP, which will fail if it isn't valid XML. Commented Aug 20, 2013 at 22:19

1 Answer 1

1

The XML which you're sending to the service is invalid. According to the XML Spec, the XML Declaration can only appear in the prolog of the document. A well-formed XML document is composed of prolog + a root element. And XML declarations are not allowed in the element node.

You may want to consider passing the document with the XML declaration as a string (e.g., inside a <!\[CDATA\[ section, or escaping the XML characters), so you'll be able to receive a XML "document" within the XML request.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.