Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect client's timeout exception on the server side?

Tags:

c#

wcf

I am using synchronous WCF service which works well 99% of the time, but on some very rare occasions client times out before the server finishes processing. Is there a way to detect, on the SERVER side, that the client has timed out? I could use async operation, but in this case server-side timeout detection would save me quite a lot of work. I'm using net.tcp binding, if this matters.

like image 625
Maciej Falkowski Avatar asked Jun 11 '15 10:06

Maciej Falkowski


People also ask

How to check timeout error?

Before using the response from the request, you can check for a timeout error using the os. IsTimeout() method. It returns a boolean indicating whether the error is known to report a timeout that occurred.

How do you handle timeout in Web services?

You can control the connection and socket timeout of the loading of the WSDL definition by setting enable-wsdl-discovery-timeouts . Use the value -1 to use the default of the underlying infrastructure. Use the value 0 to disable the timeouts. Use a positive integer to specify a timeout in milliseconds.

What happens when client timeout?

Client timeouts set a limit on how long they are willing to wait for a response to come back—and in some cases be completed. Server timeouts set a limit on how long the connection should remain open.

What are timeout exceptions?

Start Learning. In Selenium, TimeOut exception occurs when a command takes longer than the wait time to avoid the ElementNotVisible Exception. If the commands do not complete even after the wait time is over, a TimeOut Exception is thrown.


1 Answers

For net.tcp, http, etc. in general no. (see comments above for some ideas; also it might be different for other protocols/bindings/etc.)

The reason is, that the WCF infrastructure code on the server side will not use the channel before it has finished executing the service operation's implementation code and marshalling the response. Only then it will attempt to send the response and at this point recognize that the connection has already been aborted by the client.

When the server gets that error the user code (your service operation implementation) is already done and thus you cannot react to that from there anymore. It might be possible from within a dispatcher, or other extension point, but personally I have not tried. However, that also would not save your server from unnecessary work, because as said, the client disconnect will still only be recognized when the server actually attempts to send the answer.

One "simple" way to mitigate such issues might be to split the work being done into several service operations/calls (if at all possible; and not accidentally introducing server-side state in the process). Or as others have said, have the client implement a "Ping" interface that the server can use to check if the client is still "alive" and the response is still needed.

like image 169
Christian.K Avatar answered Oct 06 '22 00:10

Christian.K