现象:

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

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)

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的日志/抓包的日志都很重要。

原因

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

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

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

WCF异常原理

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

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

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

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

WCF异常System.ServiceModel.ProtocolException问题处理的更多相关文章

  1. WCF部署到IIS异常(详细: 不能加载类型System.ServiceModel.Activation.HttpModule )

    未能从程序集“System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”中加载类型“ ...

  2. 【WinForm】“System.Data.SqlClient.SqlConnection”的类型初始值设定项引发异常,无法识别的配置节 system.serviceModel

    出现问题的原因: 在本机上没有出现问题,让一个同事测试的时候,在另外一台电脑上出现连接数据库失败,系统不能打开的问题 在网上搜了一下,有说是数据库连接字符串错误的,有说app.config文件配置不匹 ...

  3. 【使用WCF,发布服务端浏览报错】未能从程序集“System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089” 中加载类型 “System.ServiceModel.Activation.HttpModule”。

    问题: 在WIN7中的IIS服务器中部署WCF服务程序时,通过浏览器访问报出如下错误: 未能从程序集"System.ServiceModel, Version=3.0.0.0, Cultur ...

  4. WCF入门教程(四)通过Host代码方式来承载服务 一个WCF使用TCP协议进行通协的例子 jquery ajax调用WCF,采用System.ServiceModel.WebHttpBinding System.ServiceModel.WSHttpBinding协议 学习WCF笔记之二 无废话WCF入门教程一[什么是WCF]

    WCF入门教程(四)通过Host代码方式来承载服务 Posted on 2014-05-15 13:03 停留的风 阅读(7681) 评论(0) 编辑 收藏 WCF入门教程(四)通过Host代码方式来 ...

  5. jquery ajax调用WCF,采用System.ServiceModel.WebHttpBinding

    Jquery ajax调用WCF服务 例子效果如下:原界面 点击按钮GetList get后,通过指定的Url获取数据添加到table 新建一个控制台项目,添加IContract.cs,DBServi ...

  6. jquery ajax调用WCF,采用System.ServiceModel.WSHttpBinding协议

    采用System.ServiceModel.WSHttpBinding或者basicHttpBinding 协议.客户端就不能直接在前端通过url直接访问服务了 它是基于SOAP协议的bing,会采用 ...

  7. WCF项目问题1-找不到类型“WCFService.Service1”,它在 ServiceHost 指令中提供为 Service 特性值,或在配置元素 system.serviceModel/serviceHostingEnvironment/serviceActivations 中提供。

    找不到类型“WCFService.Service1”,它在 ServiceHost 指令中提供为 Service 特性值,或在配置元素 system.serviceModel/serviceHosti ...

  8. System.ServiceModel.CommunicationException: 接收HTTP 响应时错误发生

    具体错误信息例如以下: System.ServiceModel.CommunicationException: 接收对 http://110.110.110.110/TestService/TestS ...

  9. 通信对象 System.ServiceModel.Channels.ServiceChannel 无法用于通信,因为其处于“出错”状态。

    通信对象 System.ServiceModel.Channels.ServiceChannel 无法用于通信,因为其处于“出错”状态. 在 System.ServiceModel.Channels. ...

  10. 未能加载文件或程序集“System.ServiceModel, Version=2.0.5.0, ...”解决方法

    未能加载文件或程序集“System.ServiceModel, Version=2.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”或它 ...

随机推荐

  1. git 安装 和 git 客户端的使用

    git clone 命令 # 查前当前登录用户的一些基本信息: # 查看当前登录的账号:git config user.name # 修改当前登录的账号为xcj:git config --global ...

  2. u8二次开发再遇问题再总结

    在调用api生成采购入库单时,报错:该操作会造成订单到货和入库同时存在,请重新检查操作!普通采购必有订单,存货[0501-0304-0075]不能手工录入 这是因为:采购入库单单据必须要录入上游单据, ...

  3. 2023 Hive 面试大纲

    先说一些废话 总结一下Hive面试宝典中的要点,方便读者快速过一遍Hive面试所需要的知识点. 本文请搭配 Hive面试宝典 来食用更美味哟 ┗( ▔, ▔ )┛ 方便自己系统性回忆,根据*的数量来标 ...

  4. 自定义动画 jquery的结束动画

      <button name="width">改变宽</button>     <button name="height"> ...

  5. git与gitee码云

    1.git分支 在前面我们基本了解Git的使用方法,这一节我们看下GIt重要概念[分支] 背景 例如于超老师在开发一个同性交友网站,刚写到登录功能,代码还没写完,今天先睡觉了,所以就commit提交到 ...

  6. SQL 如何去掉字段中千位的逗号(比如set @= '1,320.00' 想得到@= '1320.00' )

    1/去掉字段里的逗号.(比如set @= '1,320.00' 想得到@= '1320.00' )UPDATE table SET fieldA = REPLACE(fieldA, ',', '') ...

  7. OAuth + Security - 2 - 资源服务器配置

    PS:此文章为系列文章,建议从第一篇开始阅读. 资源服务器配置 @EnableResourceServer 注解到一个@Configuration配置类上,并且必须使用ResourceServerCo ...

  8. Python数据类型(数字,字符串,[列表],(元组),{字典:字典值},{列表,列表2})

    Python数据类型(数字,字符串,[列表],(元组),{字典:字典值},{列表,列表2}) # 1. # Python3 数字(Number) # Python 数字数据类型用于存储数值. # 数据 ...

  9. mybatis查询参数Set遍历查询

    #sqlmapper <resultMap id="BaseResultMap" type="com.LogEntity" > <result ...

  10. [翻译].NET 8 的原生AOT及高性能Web开发中的应用[附性能测试结果]

    原文: [A Dive into .Net 8 Native AOT and Efficient Web Development] 作者: [sharmila subbiah] 引言 随着 .NET ...