【问题标题】:ESTES ShipmentTracking v1.1 Web Service, Error in deserializing body of reply message for operation 'shipmentTracking'ESTES ShipmentTracking v1.1 Web 服务,反序列化操作“shipmentTracking”的回复消息正文时出错
【发布时间】:2019-06-13 17:00:21
【问题描述】:

我正在尝试开发一个简单的 Windows 窗体应用程序,以从最新版本的 ESTES ShipmentTracking v1.1 Web 服务请求跟踪信息。当我执行请求时,我的程序会抛出一个System.ServiceModel.CommunicationException

我正在使用 Visual Studio 2019 和 .NET 4.6.2 C# Windows 窗体应用程序。 我使用添加服务引用过程配置了Connected Service,并使用了ESTES_Track 命名空间。 这是我的基本代码:

ESTES_Track.EstesShipmentTracking_PortTypeClient trackClient = new EstesShipmentTracking_PortTypeClient();
trackClient.ClientCredentials.UserName.UserName = "MYUSERNAME";
trackClient.ClientCredentials.UserName.Password = "MYPASSWORD";
ESTES_Track.search trackSearch = new ESTES_Track.search();
trackSearch.requestID = "TRACK" + DateTime.Now.Ticks.ToString();
trackSearch.pro = "1710394802";
ESTES_Track.shipmentTrackingRequest trackRequest = new shipmentTrackingRequest(trackSearch);
ESTES_Track.shipmentTrackingResponse trackResponse = trackClient.shipmentTracking(trackRequest);

通讯异常是:

Error in deserializing body of reply message for operation 'shipmentTracking'
There is an error in XML document (1, 575)
   at System.ServiceModel.Dispatcher.XmlSerializerOperationFormatter.DeserializeBody(XmlDictionaryReader reader, MessageVersion version, XmlSerializer serializer, MessagePartDescription returnPart, MessagePartDescriptionCollection bodyParts, Object[] parameters, Boolean isRequest)
   at System.ServiceModel.Dispatcher.XmlSerializerOperationFormatter.DeserializeBody(XmlDictionaryReader reader, MessageVersion version, String action, MessageDescription messageDescription, Object[] parameters, Boolean isRequest)
   at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeBodyContents(Message message, Object[] parameters, Boolean isRequest)
   at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeReply(Message message, Object[] parameters)
   at System.ServiceModel.Dispatcher.ProxyOperationRuntime.AfterReply(ProxyRpc& rpc)
   at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
   at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

Exception rethrown at [0]:
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
   at EstesTests.ESTES_Track.EstesShipmentTracking_PortType.shipmentTracking(shipmentTrackingRequest request)
   at EstesTests.ESTES_Track.EstesShipmentTracking_PortTypeClient.shipmentTracking(shipmentTrackingRequest request) in C:\tests\EstesTests\Connected Services\ESTES_Track\Reference.cs:line 1394
   at EstesTests.Program.TrackTest() in C:\tests\EstesTests\Program.cs:line 49

-=-=-=- 编辑 -=-=-=- 我可以使用 SoapUI 成功处理请求并获得有效的响应。这让我相信我的问题是特定于我的 Visual Studio 项目的。

我的 App.config 文件如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2" />
    </startup>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="estesrtshipmenttracking_base_ws_provider_soapws_EstesShipmentTracking_Binder">
                  <security mode="Transport">
                    <transport clientCredentialType="Basic"></transport>
                  </security>
                </binding>
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="https://api.estes-express.com:443/ws/estesrtshipmenttracking.base.ws.provider.soapws:EstesShipmentTracking/estesrtshipmenttracking_base_ws_provider_soapws_EstesShipmentTracking_Port"
                binding="basicHttpBinding" bindingConfiguration="estesrtshipmenttracking_base_ws_provider_soapws_EstesShipmentTracking_Binder"
                contract="ESTES_Track.EstesShipmentTracking_PortType" name="estesrtshipmenttracking_base_ws_provider_soapws_EstesShipmentTracking_Port" />
        </client>
    </system.serviceModel>
</configuration>

-=-=-=- 编辑 -=-=-=- 我将其明确标记为“ESTES ShipmentTracking v1.1”的原因是我很确定我遇到的问题可能特定于这个特定的 Web 服务。如果有人已经有使用此 Web 服务的工作代码示例,那可能包括我的问题的解决方案。另外,我相信任何其他尝试为 ESTES 开发运输客户端的人都会遇到这个问题。

-=-=-=- 结论-=-=-=- 我得出结论,响应消息的 XML 内容不充分符合已发布的 v1.1 WSDL 模式,并且反序列化异常是特定于该 Web 服务实现版本的。之前的v1.0版本我没有遇到过这个问题。

【问题讨论】:

  • 第 1 行字符 575 上的 XML 响应有问题。您的代码在处理之前可能没有等待整个 xml。我会使用像wireshark或fiddler这样的嗅探器,看看嗅探器的实际响应是什么。
  • 我会尝试,但我认为 XML 可能会被 SSL 在线加密
  • 然后嗅探器会提示它是加密的。我不这么认为,因为错误出现在字符位置一,而不是字符 575。
  • 当我查看使用 SoapUI 发送相同请求时捕获的原始 XML 响应中的那个位置时,我看到以下 XML 元素的值中有空格:&lt;eventTimeStamp&gt;2019-05-31T19:00:00-0400 &lt;/eventTimeStamp&gt; 我想我需要以某种方式告诉反序列化器如何修剪这些空间来解决这个问题。
  • 我破解了 WCF Connected Service 代码文件并将 DateTime 类型更改为字符串。这将错误“移动”到了后面的字符位置。将所有 DateTime、日期和时间类型更改为字符串后,我现在收到 trackingInfo 反射错误。无论如何,我不认为破解代码生成器文件是一种解决方案,尤其是如果它不起作用的话。

标签: c# web-services wcf soap


【解决方案1】:

我今天遇到了同样的问题,并将其缩小到几个问题:

  • eventTimeStamp 在时区部分缺少冒号
  • 空字段缺少 xsi:nil

我写信给技术支持,但同时能够通过使用自定义消息检查器操作响应来使其工作,如https://blogs.msdn.microsoft.com/dsnotes/2015/04/14/wcf-simple-way-to-modify-serialized-response/ 所述。

您可以像这样将它添加到您的客户端:

trackClient.Endpoint.Behaviors.Add(new EstesTrackingEndpointBehavior());

希望他们能最终解决这个问题。如果出现其他问题,您可以在 AfterReceiveReply 中为响应添加其他更改:

using System;
using System.IO;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using System.Xml;

namespace EstesWebService {
    public class EstesTrackingMessageInspector : IClientMessageInspector {
        public void AfterReceiveReply(ref Message reply, object correlationState) {
            var doc = new XmlDocument();
            var ms = new MemoryStream(); 
            var writer = XmlWriter.Create(ms);
            reply.WriteMessage(writer);
            writer.Flush();
            ms.Position = 0;
            doc.Load(ms);

            //fix the XML
            addNil(doc.SelectNodes(".//shipments"));
            foreach (XmlNode node in doc.SelectNodes(".//eventTimeStamp"))
                fixDateTimeFormat(node);

            ms.SetLength(0);
            writer = XmlWriter.Create(ms);
            doc.WriteTo(writer);
            writer.Flush();
            ms.Position = 0;
            var reader = XmlReader.Create(ms);
            reply = Message.CreateMessage(reader, int.MaxValue, reply.Version);
        }

        public object BeforeSendRequest(ref Message request, IClientChannel channel) {
            return null;
        }

        private void addNil(XmlNodeList nodes) {
            foreach (XmlNode node in nodes) {
                if (node.HasChildNodes)
                    addNil(node.ChildNodes);
                else if (string.IsNullOrWhiteSpace(node.InnerText) && node.Attributes != null && node.Attributes.GetNamedItem("xsi:nil") == null) {
                    var attr = node.OwnerDocument.CreateAttribute("xsi", "nil", "http://www.w3.org/2001/XMLSchema-instance");
                    attr.Value = "true";
                    node.Attributes.SetNamedItem(attr);
                }
            }
        }

        private void fixDateTimeFormat(XmlNode node) {
            if (node != null && !string.IsNullOrWhiteSpace(node.InnerText)) {
                DateTimeOffset dt;
                if (DateTimeOffset.TryParse(node.InnerText.Trim(), out dt))
                    node.InnerText = dt.ToString("O");
            }
        }

    }

    public class EstesTrackingEndpointBehavior : IEndpointBehavior {
        public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters) {
        }

        public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime) {
            clientRuntime.MessageInspectors.Add(new EstesTrackingMessageInspector());
        }

        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) {
        }

        public void Validate(ServiceEndpoint endpoint) {
        }
    }
}

【讨论】:

  • 感谢您教我这项技术!我会试一试,然后发回我的结果。 //aj
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-09
  • 2021-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多