问题
I sometimes got error "The underlying connection was closed: A connection that was expected to be kept alive was closed by the server" when calling another WSDL service.
I use .Net 4.5 WCF service, My application connect to external 3rd party SOAP-WSDL service to perform actions. most of calls to this service done successfully, but there is one specific call, 5% of that calls are failed with that error
The underlying connection was closed: A connection that was expected to be kept alive was closed by the server
Below is the bindingConfiguration for that service
<bindings>
<customBinding>
<binding name="AnotherServiceSoap12Binding" openTimeout="01:30" receiveTimeout="01:30" sendTimeout="01:30" closeTimeout="01:30">
<textMessageEncoding maxReadPoolSize="64" maxWritePoolSize="16" messageVersion="Soap12" writeEncoding="utf-8">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="2147483647" maxBytesPerRead="4096" maxNameTableCharCount="999999" />
</textMessageEncoding>
<httpTransport manualAddressing="false"
allowCookies="false" authenticationScheme="Anonymous" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" keepAliveEnabled="true"
maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"
proxyAuthenticationScheme="Anonymous" realm="" transferMode="Buffered" unsafeConnectionNtlmAuthentication="false" useDefaultWebProxy="true" />
</binding>
</customBinding>
</bindings>
Also I notice from logs that the connection not timeout, it takes around less than 10 seconds before that error comes.
Any idea what is the problem, or how could I get more data to detect the root cause of it. also I need to know if the fix should come from my client application or from the 3rd party service.
回答1:
You should check if the client config for your client endpoint binding openTimeout="01:30" receiveTimeout="01:30" sendTimeout="01:30" closeTimeout="01:30"
is the same for your 3rd party service and also your transport need to meet the required requirement for both side.
<httpTransport manualAddressing="false" allowCookies="false" authenticationScheme="Anonymous" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" keepAliveEnabled="true" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" proxyAuthenticationScheme="Anonymous" realm="" transferMode="Buffered" unsafeConnectionNtlmAuthentication="false" useDefaultWebProxy="true" />
This error happens, when the client time out is greater than the server's time out. To avoid this error your receiveTimeout and sendTimeout should be increased.
来源:https://stackoverflow.com/questions/48749480/the-underlying-connection-was-closed-a-connection-that-was-expected-to-be-kept