我有一个具有以下签名的小WCF服务:
string ProcessMessage(XmlElement xmlSource);
当我收到没有xml声明的请求时,webservice工作正常,但是一旦添加了xml声明,我就会崩溃。
有效的请求
<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>
不起作用的请求
<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>
崩溃:
<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>
我假设正在发生的是WCF服务正在将整个soap信封作为一个xml文档读取,然后当它遇到xml声明(对于参数xmlSource
)并将其解释为xml文档时整个肥皂信封。这就是它崩溃的地方,并说xml声明必须是文档的第一部分。
有没有办法解决这个问题,或者在请求到达Web服务之前从请求中删除xml声明?
答案 0 :(得分:1)
您发送给服务的XML无效。根据{{3}},XML声明只能出现在文档的序言中。 XML Spec由well-formed XML document + prolog组成。元素节点中不允许使用XML声明。
您可能需要考虑将带有XML声明的文档作为字符串传递(例如,在a root element内部,或者转义XML字符),这样您就可以在其中接收XML“文档” XML请求。