专栏首页.NET开发那点事关于WCF服务在高并发情况下报目标积极拒绝的异常处理

关于WCF服务在高并发情况下报目标积极拒绝的异常处理

最近弄了个wcf的监控服务,偶尔监控到目标服务会报一个目标积极拒绝的错误。一开始以为服务停止了,上服务器检查目标服务好好的活着。于是开始查原因。

一般来说目标积极拒绝(TCP 10061)的异常主要是2种可能:

1:服务器关机或者服务关闭

2:Client调用的端口错误或者服务器防火墙没开相应的端口

但是我们的服务本身是可以调用的,只是偶尔报这个错误,说明并不是这2个问题造成的。继续google,在stackoverflow上看到这样一篇:传送门

If this happens always, it literally means that the machine exists but that it has no services listening on the specified port, or there is a firewall stopping you.

If it happens occasionally - you used the word "sometimes" - and retrying succeeds, it is likely because the server has a full 'backlog'.

When you are waiting to be accepted on a listening socket, you are placed in a backlog. This backlog is finite and quite short - values of 1, 2 or 3 are not unusual - and so the OS might be unable to queue your request for the 'accept' to consume.

The backlog is a parameter on the listen function - all languages and platforms have basically the same API in this regard, even the C# one. This parameter is often configurable if you control the server, and is likely read from some settings file or the registry. Investigate how to configure your server.

If you wrote the server, you might have heavy processing in the accept of your socket, and this can be better moved to a separate worker-thread so your accept is always ready to receive connections. There are various architecture choices you can explore that mitigate queuing up clients and processing them sequentially.

Regardless of whether you can increase the server backlog, you do need retry logic in your client code to cope with this issue - as even with a long backlog the server might be receiving lots of other requests on that port at that time.

There is a rare possibility where a NAT router would give this error should it's ports for mappings be exhausted. I think we can discard this possibility as too much of a long shot though, since the router has 64K simultaneous connections to the same destination address/port before exhaustion.

大概意思就是如果这个错误是一直发生的那么可能是服务器或者防火墙的问题,如果这个问题是“Sometime”发生的,那么可能是backlog的问题。backlog是tcp层面的请求队列,当你调用socket发起请求的时候服务端会排成一个队列,在高并发情况下服务端来不及处理请求,那么有些请求就被直接被丢弃,于是就报了目标积极拒绝TCP10061的异常。

有了backlog于是继续google关键字“WCF backlog”发现wcf binding配置确实有一个listenBacklog的项目,默认值是10,于是把服务的listenBacklog改成100,问题搞定。

对了添加listenBacklog属性的时候有个注意的是一定要移除一个默认的endpoint      <endpoint address="mex" binding="mexTcpBinding" bindingConfiguration="" contract="IMetadataExchange" />这个endpoint是用来给vs等发现元数据用的,如果这个不移走启动服务的时候会报端口已经被监听的错误。

参考:http://stackoverflow.com/questions/2972600/no-connection-could-be-made-because-the-target-machine-actively-refused-it

https://msdn.microsoft.com/en-us/library/ee377061(v=bts.10).aspx

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • Async方法死锁的问题 Don't Block on Async Code(转)

    今天调试requet.GetRequestStreamAsync异步方法出现不返回的问题,可能是死锁了。看到老外一篇文章解释了异步方法死锁的问题,懒的翻译,直接...

    kklldog
  • If WCF Service side and Client side config is different?!

    from stackoverflow  http://stackoverflow.com/questions/4879310/when-setting-up-a...

    kklldog
  • 使用Azure DevOps Pipeline实现.Net Core程序的CI

    上次介绍了Azure Application Insights,实现了.net core程序的监控功能。这次让我们来看看Azure DevOps Pipelin...

    kklldog
  • OpenCV 利用getTickCount()与getTickFrequency()计算执行时间

    其实这是个很简单的应用,贴出来是因为我经常能用到这两个函数,顺便写一下吧。 double t1 = (double)getTickCount(); . . . ...

    chaibubble
  • 代码环复杂度的计算公式

    Why calculated as 3? See the directed graph for this simple report below:

    Jerry Wang
  • 一个好用的SAP ABAP工作进程跟踪工具

    As an ABAPer we have SAT, ST05 ( or sometimes ST12 ) for trace in our toolbox, a...

    Jerry Wang
  • 不完全免疫算法简介IMADE--AIS学习笔记5

    DrawSky
  • 【转】面试官必读:Before you are going to interview candidates…

    Here is some tips before you are going to interview new candidates:

    崔文远TroyCui
  • SAP UI5应用如果遇到数据绑定问题时,应该如何自己定位问题?

    I will use the field “ClosingDate” to demonstrate my favorite approach to troubl...

    Jerry Wang
  • 编码机器的解读(cs.LG)

    机器学习过程中训练的模型可能会存储训练过程中使用的单个样本的信息。在许多情况下,可能需要删除单个样本的影响,并从模型中解除学习(即删除)。从训练集中删除样本后,...

    用户8078797

扫码关注云+社区

领取腾讯云代金券