经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » ASP.net » 查看文章
WCF异常System.ServiceModel.ProtocolException问题处理
来源:cnblogs  作者:时风拖拉机  时间:2024/7/19 10:38:21  对本文有异议

现象:

最近遇到了WCF 服务无法调用的错误,异常如下。

  1. System.ServiceModel.ProtocolException, System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 Error while reading message framing format at position 0 of stream (state: ReadingUpgradeRecord)
  2. System.IO.InvalidDataException, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 More data was expected, but EOF was reached.

日志

WCF的问题一般需要对框架比较熟悉,有时难以定位。第一眼找不到原因的先 根据官方文档打开WCF日志

日志打开后发现服务端完全没有调用的记录。这时怀疑是不是调错端口了,打开资源管理器,服务正常跑着。只能再打开Wireshark记录日志,大概就是正常的3次握手,不正常的不知为何服务端主动FIN并RESET的连接。

在WCF的问题处理中,自己的日志/WCF的日志/抓包的日志都很重要。

原因

最后的原因发现是端口被罗技升级程序占用的问题, 考虑如下示例代码

  1. // 设置要监听的端口号
  2. int port = 13000;
  3. TcpListener listener = new TcpListener(IPAddress.Any, port);
  4. TcpListener listener2 = new TcpListener(IPAddress.Loopback, port);
  5. // 开始监听
  6. listener.Start();
  7. listener2.Start();
  8. // 并不会抛出异常
  9. Console.ReadLine();

工作久了过于相信经验,感觉一定会异常,但端口监听的是套接字,自然0.0.0.0:13000 和 127.0.0.1:13000不是一回事。

WCF异常原理

wcf的通信流程大概是这样的。

  1. // 客户端发送服务端基地址,确认这是一个wcf的服务
  2. client-> service: net.tcp://127.0.0.1:39100/Service
  3. // 服务端回0b 代表确认
  4. service-> client: 0x0b
  5. // 客户端发送具体调用的内容
  6. .....

由于服务端不回 0x0b,自然会出现这样的异常。附一下异常的调用栈。事后看看也挺明显。

  1. System.ServiceModel.Channels.ConnectionUpgradeHelper.ValidatePreambleResponse(Byte[] buffer, Int32 count, ClientFramingDecoder decoder, Uri via)
  2. System.ServiceModel.Channels.ClientFramingDuplexSessionChannel.SendPreamble(IConnection connection, ArraySegment`1 preamble, TimeoutHelper& timeoutHelper)
  3. System.ServiceModel.Channels.ClientFramingDuplexSessionChannel.DuplexConnectionPoolHelper.AcceptPooledConnection(IConnection connection, TimeoutHelper& timeoutHelper)
  4. System.ServiceModel.Channels.ConnectionPoolHelper.EstablishConnection(TimeSpan timeout)
  5. System.ServiceModel.Channels.ClientFramingDuplexSessionChannel.OnOpen(TimeSpan timeout)
  6. System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
  7. System.ServiceModel.Channels.ServiceChannel.OnOpen(TimeSpan timeout)
  8. System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
  9. System.ServiceModel.Channels.ServiceChannel.CallOpenOnce.System.ServiceModel.Channels.ServiceChannel.ICallOnce.Call(ServiceChannel channel, TimeSpan timeout)
  10. System.ServiceModel.Channels.ServiceChannel.CallOnceManager.CallOnce(TimeSpan timeout, CallOnceManager cascade)
  11. System.ServiceModel.Channels.ServiceChannel.EnsureOpened(TimeSpan timeout)
  12. System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
  13. System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
  14. System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
  15. System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
  16. ...

原文链接:https://www.cnblogs.com/zhangchen-trunk/p/18310625

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号