简体   繁体   中英

What is the best way to check clients connection in WCF

I have a WCF service and number of clients which retrieve information about credentials. I want to monitor list of these clients and detect if some of the clients suddenly die (due to connection loss or application crash or something) What is the best way to do this ?

As I understand duplex communication contract would not be the best solution.

One way to do this would be to implement a "heartbeat". To do this, keep a collection of clients. Have the client send a simple message with minimal information (a heartbeat message).

On the server side, put an eviction process in place that periodically looks through the list of clients to see if there are any that have gone "stale" (ie you haven't received a heartbeat in a while).

There is really no good way to do this apart from catching a CommunicationException . You can also look at the IClientChannel events to monitor what happens with the connection.

client.InnerChannel.Closed += OnChannelClosed;
client.InnerChannel.Opening += OnChannelOpening;
client.InnerChannel.Opened += OnChannelOpened;
client.InnerChannel.Closing += OnChannelClosing;
client.InnerChannel.Faulted += OnChannelFaulted;
client.InnerChannel.UnknownMessageReceived += OnChannelUnknownMessageReceived;

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