解决WCF响应中的无效字节序列

时间:2015-08-24 09:19:46

标签: c# wcf

我从中检索数据的WCF端点已经开始返回包含无效字节序列的响应。不幸的是,这不会被固定在上游,直到下一个发布周期,所以在那之前,我需要解决它;即我需要修改我的WCF客户端,以便它忽略从服务器返回的无效字节序列,而不是崩溃和刻录。

System.ServiceModel.CommunicationException: Error in deserializing body of reply message for operation 'getTerminationPointsIterator'. ---> System.InvalidOperationException: There is an error in XML document (1, 14291602). ---> System.Xml.XmlException: The byte 0x13 is not valid at this location.  Line 1, position 14291602.
   at System.Xml.XmlExceptionHelper.ThrowXmlException(XmlDictionaryReader reader, String res, String arg1, String arg2, String arg3)
   at System.Xml.XmlExceptionHelper.ThrowInvalidXml(XmlDictionaryReader reader, Byte b)
   at System.Xml.XmlUTF8TextReader.Read()
   at System.Xml.XmlLoader.LoadNode(Boolean skipOverWhitespace)
   at System.Xml.XmlLoader.ReadCurrentNode(XmlDocument doc, XmlReader reader)
   at System.Xml.XmlDocument.ReadNode(XmlReader reader)
   at System.Xml.Serialization.XmlSerializationReader.ReadXmlNode(Boolean wrapped)
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderTerminationPointRetrieval_RPC.Read21_AnyListType(Boolean isNullable, Boolean checkType)
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderTerminationPointRetrieval_RPC.Read64_PhysicalTerminationPointType(Boolean isNullable, Boolean checkType)
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderTerminationPointRetrieval_RPC.Read86_TerminationPointType(Boolean isNullable, Boolean checkType)
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderTerminationPointRetrieval_RPC.Read93_Item(Boolean isNullable, Boolean checkType)
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderTerminationPointRetrieval_RPC.Read248_Item()
   at Microsoft.Xml.Serialization.GeneratedAssembly.ArrayOfObjectSerializer122.Deserialize(XmlSerializationReader reader)
   at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
   --- End of inner exception stack trace ---
   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)

首先,我尝试实施IClientMessageInspector#AfterReceiveReply,尝试过滤Message中的无效字符;

public void AfterReceiveReply(ref Message reply, object correlationState)
{
    Message revised = null;
    var contents = new StringBuilder();
    var filtered = new StringBuilder();
    var writer = XmlWriter.Create(contents, new XmlWriterSettings
    {
        CheckCharacters = false
    });

    reply.WriteMessage(writer);
    writer.Flush();

    foreach (var c in contents.ToString())
    {
        if (XmlConvert.IsXmlChar(c))
        {
            filtered.Append(c);
        }
    }

    revised = Message.CreateMessage(XmlReader.Create(new StringReader(filtered.ToString())), Int32.MaxValue, reply.Version);
    revised.Properties.CopyProperties(reply.Properties);
    reply = revised;
}

...但是这会在reply.WriteMessage(writer)行引发相同的错误:

  

类型' System.Xml.XmlException'的例外情况发生在System.Runtime.Serialization.dll中但未在用户代码中处理

     

附加信息:字节0x13在此位置无效。第1行,位置14291602。

有人可以建议我的客户端优雅地解析无效响应的方法,过滤掉无效的XML字符吗?

0 个答案:

没有答案