【问题标题】:WCF .Net 4.0 Client Error in deserializing body of reply messageWCF .Net 4.0 客户端在反序列化回复消息正文时出错
【发布时间】:2010-07-26 22:44:57
【问题描述】:

我正在尝试让 WCF .Net 4.0 客户端与 NuSOAP/0.7.2 (1.94) Web 服务一起使用。首先,Web 服务不是基于当前的 SOA 标准,所以我必须创建一个自定义编码器来处理 ISO-8859-1 消息编码。当我得到响应时,WCF 抛出以下异常

“反序列化回复消息正文时出错”

查看 SOAP 主体时,我看到了 <SOAP-ENV:Body>... stream ...</SOAP-ENV:Body>

有人遇到过这个错误吗?任何反馈或正确方向的观点将不胜感激。这是我的第一个 WCF 到“非”WCF 服务。

ApplicationData
TraceData
<DataItem>
<MessageLogTraceRecord Time="2010-07-14T19:14:36.4832868-07:00" Source="TransportReceive" Type="System.ServiceModel.Channels.StreamedMessage" xmlns="http://schemas.microsoft.com/2004/06/ServiceModel/Management/MessageTrace">
<HttpResponse>
<StatusCode>OK</StatusCode>
<StatusDescription>OK</StatusDescription>
<WebHeaders>
<X-SOAP-Server>NuSOAP/0.7.2 (1.94)</X-SOAP-Server>
<Content-Encoding></Content-Encoding>
<Content-Length>496</Content-Length>
<Content-Type>text/xml; charset=ISO-8859-1</Content-Type>
<Date>Thu, 15 Jul 2010 02:14:36 GMT</Date>
<Server>Apache/2.0.54 (Unix) mod_ssl/2.0.54 OpenSSL/0.9.7a PHP/4.3.11</Server>
<X-Powered-By>PHP/4.3.11</X-Powered-By>
</WebHeaders>
</HttpResponse>
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="https://www.datatranswebedi.com/soapv2/">
<SOAP-ENV:Header></SOAP-ENV:Header>
<SOAP-ENV:Body>... stream ...</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
</MessageLogTraceRecord>
</DataItem>
</TraceData>
</ApplicationData>

提前致谢, 布伦南

【问题讨论】:

    标签: wcf encoding .net-4.0 client nusoap


    【解决方案1】:

    结果在反序列化日期时出错,因为它的格式不正确。我发现错误是在 WCF 客户端中启用“详细”调试模式。

    System.InvalidCastException:无法将 System.Xml.XmlNode[] 类型的对象分配给 System.DateTime 类型的对象。

    旧版 Web 服务正在传回“xsd:date”以及数据类型应为“xsd:dateTime”的日期和时间。

    为了确认这是问题的原因,我从 Web 服务请求了两条记录,并在重写 Message ReadMessage() 方法中操作了 XML,并更改了自动生成的代理类。这纠正了问题,我知道这不是最终的解决方案。在我联系旧版 Web 服务的作者之前,是否有人对 Soap 日期和 dateTime 格式有任何 cmet?

    Maybe the following date is valid in Soap Version 1.1 with ISO-8859-1 encoding:
    
    <date xsi:type="xsd:date">2010-05-02 08:03:45</date>
    
     If it is, is this an issue using a custom encoder or was it really the date type/format in the soap body? Microsoft only accepted that date in the following format:
    
    <date xsi:type="xsd:dateTime">2010-05-02T08:03:45</date>
    
    
        <customBinding>
          <binding name="ISO8859Binding">
            <customTextMessageEncoding messageVersion="Soap11" encoding="ISO-8859-1" mediaType="text/xml"  />
            <httpsTransport />
          </binding>
          </customBinding>
    
    public override Message ReadMessage(Stream stream, int maxSizeOfHeaders, string contentType)
    {
    
        XmlReader reader = XmlReader.Create(stream);
        XmlDocument doc = new XmlDocument();
    
        // Log to a file.
        StreamWriter sw = new StreamWriter(@"C:\dump2.txt");
    
        doc.Load(reader);
        doc.InnerXml = doc.InnerXml.Replace("xsd:date", "xsd:dateTime");
        doc.InnerXml = doc.InnerXml.Replace("2010-05-02 08:03:45", "2010-05-02T08:03:45");
        doc.InnerXml = doc.InnerXml.Replace("2010-05-02 12:38:06", "2010-05-02T12:38:06");
    
        sw.WriteLine(doc.InnerXml);
        sw.Flush();
        sw.Close();
    
        byte[] buffer = System.Text.Encoding.Default.GetBytes(doc.InnerXml);
        Stream test = new MemoryStream(buffer);
        XmlReader reader2 = XmlReader.Create(test);
    
        return Message.CreateMessage(reader2, maxSizeOfHeaders, this.MessageVersion);
    
    }
    

    再次感谢, 布伦南

    【讨论】:

      【解决方案2】:

      我不得不做同样的事情(针对另一个遗留类型的 web 服务),我一开始遇到了问题。

      我看到了这篇出色的博客文章 WCF: Text Message Encoding and ISO-8859-1 Encoding,它描述了为 WCF 实现自定义文本编码器时的一些陷阱 - 也许对您有帮助?

      还可以查看之前关于同一主题的 Stackoverflow 问题:

      Calling a webservice that uses ISO-8859-1 encoding from WCF

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-09-10
        • 1970-01-01
        • 1970-01-01
        • 2011-12-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-16
        相关资源
        最近更新 更多