Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
305 views
in Technique[技术] by (71.8m points)

c# - WCF Not Timing out correctly?

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...