Shahid Iqbal
Shahid Iqbal

Reputation: 56

WCF: The underlying connection was closed: The connection was closed unexpectedly

As this is very common problem but there is no proper solution which can fix this issue. I am also experiencing this "The underlying connection was closed: The connection was closed unexpectedly" problem for my WCF web services but its random and there is no specific scenario through which I can reproduce. I have been trying to fix this problem since couple of weeks and tried all possible solutions including increasing the timeout, enable/disable the keep-alive connection, open a new connection for each request and then close right after completion of the request but there is no luck. I have also enabled tracing and logging at both server and client side but couldn’t find such specific issue which may cause of this problem.

These WCF SOAP web services deployed on Windows server environment using the IIS 7 and .Net Framework 4.0

Client Side Tracing:

System.Net.Sockets Verbose: 0 : [351180] 00000000 :                                                 : 
    DateTime=2017-01-20T14:25:44.4000839Z
System.Net.Sockets Verbose: 0 : [351180] Exiting Socket#34051556::Receive()     -> Int32#0
    DateTime=2017-01-20T14:25:44.4000839Z
System.Net.Sockets Verbose: 0 : [351180] Socket#34051556::Dispose()
    DateTime=2017-01-20T14:25:44.4000839Z
System.Net Error: 0 : [351180] Exception in HttpWebRequest#40245115:: - The underlying connection was closed: The connection was closed unexpectedly..
    DateTime=2017-01-20T14:25:44.4157088Z
System.Net Error: 0 : [351180] Exception in HttpWebRequest#40245115::GetResponse - The underlying connection was closed: The connection was closed unexpectedly..
    DateTime=2017-01-20T14:25:44.4157088Z

Server Side Tracing:

System.Net.Sockets Verbose: 0 : [10808] Exiting DNS::GetHostByName()    -> IPHostEntry#31978062
    DateTime=2017-01-20T14:16:20.7036270Z
System.Net.Sockets Verbose: 0 : [10808] Exiting DNS::GetHostAddresses()     -> IPAddress[]#52697188
    DateTime=2017-01-20T14:16:20.7036270Z
System.Net.Sockets Verbose: 0 : [10808] DNS::GetHostAddresses()
    DateTime=2017-01-20T14:25:03.3938764Z
System.Net.Sockets Verbose: 0 : [10808] DNS::GetHostByName()
    DateTime=2017-01-20T14:25:03.3938764Z
System.Net.Sockets Verbose: 0 : [10808] Exiting DNS::GetHostByName()    -> IPHostEntry#39699746
    DateTime=2017-01-20T14:25:03.4094763Z
System.Net.Sockets Verbose: 0 : [10808] Exiting DNS::GetHostAddresses()     -> IPAddress[]#12400315
    DateTime=2017-01-20T14:25:03.4094763Z

Tracing Config:

<system.diagnostics>
    <trace autoflush="true" />
    <sources>
            <source name="System.ServiceModel" 
                    switchValue="Critical, Error, Warning, Verbose , Information, ActivityTracing"
                    propagateActivity="true">
            <listeners>
               <add name="sdt" 
                   type="System.Diagnostics.XmlWriterTraceListener" 
                   initializeData= "c:\stacktrace_log.log" />
            </listeners>
         </source>
    </sources>
</system.diagnostics>

I would appreciate if anyone can help to resolve this issue.

Upvotes: 1

Views: 2045

Answers (1)

Shahid Iqbal
Shahid Iqbal

Reputation: 56

Finally, I have figured this issue out. The problem was with the load balancer's connection timeout which drops the connection when request takes longer than the limit defined in the load balancer. Thank you everyone for your help.

Upvotes: 1

Related Questions