WCF:套接字连接已中止。远程主机

时间:2015-07-29 18:03:58

标签: c# wcf service timeout

我一直在努力解决WCF的问题。我有一个由控制台应用程序调用的WCF服务(托管为Windows服务)。它工作得很好,但是最近我们在10分钟时遇到了超时问题,当时我们不得不针对遗留系统运行查询。

我在服务和客户端.exe中使用log4net,奇怪的是服务实际上完成了作业并且没有抛出任何异常。它运行查询(大约需要12分钟,并创建一个文件,并记录成功)。

然而,控制台应用程序会记录一个例外,声称"主机"返回超时或错误。我认为它是超时而不是缓冲区问题,因为异常总是在最初进行调用后的10分钟到第二秒发生。

以下是我的log4net的异常/堆栈信息:

2015-07-28 17:35:47,364 [1] 
System.ServiceModel.CommunicationException: The socket connection was aborted. This could be caused by an error processing your message or a receive timeout being exceeded by the remote host, or an underlying network resource issue. Local socket timeout was '00:29:59.9843999'. ---> System.IO.IOException: The read operation failed, see inner exception. ---> System.ServiceModel.CommunicationException: The socket connection was aborted. This could be caused by an error processing your message or a receive timeout being exceeded by the remote host, or an underlying network resource issue. Local socket timeout was '00:29:59.9843999'. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
   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.ConnectionStream.Read(Byte[] buffer, Int32 offset, Int32 count)
   at System.Net.FixedSizeReader.ReadPacket(Byte[] buffer, Int32 offset, Int32 count)
   at System.Net.Security.NegotiateStream.StartFrameHeader(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.NegotiateStream.ProcessRead(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)
   --- End of inner exception stack trace ---
   at System.Net.Security.NegotiateStream.ProcessRead(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.NegotiateStream.Read(Byte[] buffer, Int32 offset, Int32 count)
   at System.ServiceModel.Channels.StreamConnection.Read(Byte[] buffer, Int32 offset, Int32 size, TimeSpan timeout)
   --- End of inner exception stack trace ---

Server stack trace: 
   at System.ServiceModel.Channels.StreamConnection.Read(Byte[] buffer, Int32 offset, Int32 size, TimeSpan timeout)
   at System.ServiceModel.Channels.SessionConnectionReader.Receive(TimeSpan timeout)
   at System.ServiceModel.Channels.SynchronizedMessageSource.Receive(TimeSpan timeout)
   at System.ServiceModel.Channels.TransportDuplexSessionChannel.Receive(TimeSpan timeout)
   at System.ServiceModel.Channels.TransportDuplexSessionChannel.TryReceive(TimeSpan timeout, Message& message)
   at System.ServiceModel.Dispatcher.DuplexChannelBinder.Request(Message message, TimeSpan timeout)
   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 FileGeneratorConsole.FileGeneratorServiceReference.IFileGeneratorService.GenerateXmlFileFromSql(String sqlServer, String sqlDatabase, String sqlQuery, Int32 commandTimeout, Boolean allowEmptyResult, String outputFileName, String xslFileName, Boolean archiveExistingFile)
   at FileGeneratorConsole.Program.Main(String[] args)

现在,我以为我已经完成了我的研究,并找出了问题所在。我假设是sendTimeout和receiveTimeout是绑定定义的一部分,所以我添加了:

<bindings>
      <netTcpBinding>
        <binding name="NetTcpBinding_IFileGeneratorService"
                  maxBufferPoolSize="2147483647"
                 maxBufferSize="2147483647"
                 maxReceivedMessageSize="2147483647"
                 receiveTimeout="00:30:00"
                 closeTimeout="00:30:00"
                 openTimeout="00:30:00"
                 sendTimeout="00:30:00">
        </binding>
      </netTcpBinding>
    </bindings>

而且,如果您在上述例外中发现,您会看到错误说明:

本地套接字超时为&#39; 29:59:59.9843999&#39;

过去常说00:09:59 ....所以看起来就好了。

我还将此添加到服务配置文件中,以防双方需要绑定,但这没有帮助。

最后,出于绝望,我还添加了一些其他内容。我尝试将超时添加到服务部分:

我尝试添加

行为本身。

我现在不知所措,它总是在10分钟后死亡。

提前致谢

1 个答案:

答案 0 :(得分:5)

它总比你想象的简单。

可悲的是,一直缺少的是service.endpoint中的bindingConfiguration引用。此时,假定使用默认绑定定义运行服务。我的配置中定义了绑定,但错过了您指定的部分:

我有:

 <endpoint address="" binding="netTcpBinding" bindingConfiguration="">

我需要:

 <endpoint address="" binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IFileGeneratorService">

一旦到位,我的实际服务端点就会查看正确的绑定属性。