当应用程序创建大量 WCF 连接器类时,与服务器的所有连接都会超时

本文关键字:服务器 连接 超时 创建 应用程序 WCF 连接器 | 更新日期: 2023-09-27 17:56:02

我有一个WCF net.tcp服务器,它公开了一个具有2种方法的服务:

string Get(location);
void Put(location);

maxConnections设置为 int.MaxValue 。在客户端,maxConnections设置为 800。服务器上未引发异常

我有一个客户端应用程序,它有时会创建 100+ 个线程,每个线程都可以随时连接到服务器。

当线程共享连接器类时,应用工作正常。当我让每个线程创建自己的连接器时,当我使用 20-30 个线程时,每个线程都可以正常工作。但是,当应用创建 50+ 个线程时,所有连接突然开始超时。

System.TimeoutException: The open operation did not complete within the allotted timeout of 00:01:00. The time allotted to this operation may have been a portion of a longer timeout. ---> System.TimeoutException: The socket transfer timed out after 00:01:00. You have exceeded the timeou
t set on your binding. The time allotted to this operation may have been a portion of a longer timeout. ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected
 host has failed to respond
   at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
   at System.ServiceModel.Channels.SocketConnection.ReadCore(Byte[] buffer, Int32 offset, Int32 size, TimeSpan timeout, Boolean closing)
   --- End of inner exception stack trace ---
   at System.ServiceModel.Channels.SocketConnection.ReadCore(Byte[] buffer, Int32 offset, Int32 size, TimeSpan timeout, Boolean closing)
   at System.ServiceModel.Channels.SocketConnection.Read(Byte[] buffer, Int32 offset, Int32 size, TimeSpan timeout)
   at System.ServiceModel.Channels.DelegatingConnection.Read(Byte[] buffer, Int32 offset, Int32 size, TimeSpan timeout)
   at System.ServiceModel.Channels.ClientFramingDuplexSessionChannel.SendPreamble(IConnection connection, ArraySegment`1 preamble, TimeoutHelper& timeoutHelper)
   at System.ServiceModel.Channels.ClientFramingDuplexSessionChannel.DuplexConnectionPoolHelper.AcceptPooledConnection(IConnection connection, TimeoutHelper& timeoutHelper)
   at System.ServiceModel.Channels.ConnectionPoolHelper.EstablishConnection(TimeSpan timeout)
   at System.ServiceModel.Channels.ClientFramingDuplexSessionChannel.OnOpen(TimeSpan timeout)
   --- End of inner exception stack trace ---
Server stack trace:
   at System.ServiceModel.Channels.ClientFramingDuplexSessionChannel.OnOpen(TimeSpan timeout)
   at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannel.OnOpen(TimeSpan timeout)
   at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannel.CallOpenOnce.System.ServiceModel.Channels.ServiceChannel.ICallOnce.Call(ServiceChannel channel, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannel.CallOnceManager.CallOnce(TimeSpan timeout, CallOnceManager cascade)
   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 MySource.Get(String account)
   at MyService.Get(String account) in d:'Users'menkaur'Documents'Expression'Blend 4'Projects'Service References'MySourceService'Reference.cs:line 99
   at auto_liker_bot.Program.<>c__DisplayClass58.<>c__DisplayClass60.<Main>b__4c(IMySource _mySource) in d:'Users'menkaur'Documents'Expression'Blend 4'Projects'my-project'Program.cs:line 473
   at auto_liker_bot.Program.PerformAction(UInt64 threadId, IMySource& mySource, Action`1 action) in d:'Users'menkaur'Documents'Expression'Blend 4'Projects'my-project'Program.cs:line 919

这不是我第一次注意到WCF类会发生这样的事情。

这是 WCF 的硬性限制,还是可以更改设置以摆脱此行为?

当应用程序创建大量 WCF 连接器类时,与服务器的所有连接都会超时

您遇到的是客户端到主机的连接数限制。它不是由 WCF 设置的,但 WCF 也不能幸免于此。您将需要增加它。退房:

  • ServicePointManager.DefaultConnectionLimit(如果要在代码中设置它)
  • 配置文件中的连接管理元素

基本上,默认情况下,您只有 2 个连接到同一主机。随着线程数量的增加,连接请求会增加并排队,但由于只有 2 个可用,因此在某些时候您开始超时。