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

时间:2013-01-26 13:37:42

标签: c# wcf net.tcp

我有一个WCF net.tcp服务器,它使用两种方法公开服务:

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的一个严格限制,还是有一个我可以改变以摆脱这种行为的设置?

1 个答案:

答案 0 :(得分:2)

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

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