Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am having a timeout issue, these are the details:

My binding configuration looks like this:

<netTcpBinding>
 <binding name="WindowsServerOverTcp"
   maxReceivedMessageSize="10000000"
   maxBufferSize="10000000"
   maxBufferPoolSize="10000000"
   closeTimeout="00:00:03"
  openTimeout="00:00:03"
  sendTimeout="00:00:03"
  receiveTimeout="00:00:03">
  <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
  maxArrayLength="2147483647" maxBytesPerRead="2147483647"
  maxNameTableCharCount="2147483647" />
  <security mode="None">
  </security>
 </binding>
 </netTcpBinding>

I am sending a message to a server which I know is turned off so the connection should just time out after 3 seconds as stipulated in my app.config, but for some reason it is taking 20-30 seconds.

When the EndPointNotFoundException is thrown this is the info I get:

System.ServiceModel.EndPointNotFoundException: Could not connect to net.tcp://10.0.0.82:4466/MegaMatcherWcf. The connection attempt lasted for a time span of 00:00:03. TCP error code 10060: 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 10.0.0.82:4466

If I try the same test with the machine turned on, but no listening software running I get the expected behaviour, with the connection timing out after 3 seconds. Why if the machine is off does it take 30 seconds, and then tell me it took 3 seconds?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
287 views
Welcome To Ask or Share your Answers For Others

1 Answer

To be a bit more specific than @BiggsTRC (whose answer is broadly correct):

  • WCF delegates to the System.Net.Sockets classes the details of establishing the transport layer connection in a NetTcpBinding channel;
  • System.Net.Sockets is a wrapper around the unmanaged WINSOCK API;
  • The WINSOCK API has internal default timeouts but does not provide any documented mechanism for specifying a timeout on certain blocking operations, including WSAConnect(), which the .NET Socket.Connect() method uses;
  • The WCF code (in System.ServiceModel.Channels.SocketConnectionInitiator.Connect()) calls Socket.Connect, and if this throws an exception of certain types, it checks to see whether there is any remaining time in its connection timeout period. If there isn't, you get an EndpointNotFoundException with the error message you have seen;
  • WCF uses a TimeoutHelper class to keep track of timeout periods and do the time arithmetic. This has a method called ElapsedTime, but this is a misnomer since it never returns a value greater than the original timeout period: that is the reason the error message lies to you about how long the connection attempt took.

WCF could enforce its configured timeouts by using the asynchronous methods of the Sockets API, monitoring the timeout on a separate thread to the connect attempt, but it does not currently do so. If you think this is a bug (which arguably it is) you could report it on Microsoft's Connect Site and perhaps get it fixed in a future version or service pack.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...