简体   繁体   中英

WCF Catch Asynch Exception

I have a situation I'm looking for advice on.

I have a WCF service where in some of the methods I create an object that in turn establishes a socket connection to a server. I "cache" these connection objects in a dictionary object that's "global" to my service class. Once these connections are established there is a timer associated with each of them and the timer callback sends a keep-alive message to the server at regular intervals.

The problem comes if for whatever reason the server connection gets broken. When that happens I get an unhandled exception that seems to kill the WCF service.

I am hoping there's a way I can catch these timer related exceptions and simply close the socket and remove the connection object from the cache but I'm not sure there is a way. It would seem once I leave the method that uses the connection object there's nothing to catch the asynchronous exception.

You need to develop some kind of connection manager, that holds and monitor the state of each connection. the keep-alive timer is good for state tracking but you still need to check that a connection is valid before using it.

in any case, getting exception when using a faulted connection should not result in an unhandeled exception from the WCF service, you should wrap the access to the connection manager in a try-catch clauses.

Cheers,

Gilad

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM