我有一个具有以下签名的小 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 服务正在将整个肥皂信封作为一个 xml 文档读取,然后当它遇到 xml 声明(对于参数xmlSource
)并将其解释为整个肥皂信封的 xml 文档时。这是它崩溃的地方,并说 xml 声明必须是文档的第一部分。
有没有办法解决这个问题或在请求到达 web 服务之前从请求中删除 xml 声明?