Detect socket disconnect in WCF
Asked Answered
K

2

18

We're building a WCF server (.NET 4.0). It will only use net.tcp transport.

When a client closes the TCP connection, the server gets unhandled CommunicationException, and terminates.

Q1. How do I handle the CommunicationException so the server does not terminate and continues serving other clients?

Q2. In the handler, how do I get SessionId of the session that was aborted? I need this to do clean up some session-specific data.

Thanks in advance!

P.S. The connection is over the Internet, so the socket may be closed anytime, regardless on whether the client disconnects gracefully, or not.

Kedron answered 17/3, 2011 at 12:12 Comment(2)
what are the classes that you're using? And what methods of the classes are you using to read and write? There's a couple of classes for handling TCP connections. Just to name a few, Socket, TcpClientPostdoctoral
The sockets are hidden deep inside the WCF framework. In some clients, I use System.ServiceModel.ChannelFactory<TChannel> generic class to connect to the server.Kedron
V
25

Any WCF channel implements ICommunicationObject , which provides events for the channel lifetime.

You should listen to the Faulted event

The sessionId can be accessed as always from the OperationContext.Current property.

When your client open the channel (on the first operation), register to the adequate events:

OperationContext.Current.Channel.Faulted += new EventHandler(Channel_Faulted);
OperationContext.Current.Channel.Closed += new EventHandler(Channel_Faulted);

and:

 void Channel_Faulted(object sender, EventArgs e)
 {
     Logout((IContextChannel)sender);
 }

 protected void Logout(IContextChannel channel)
 {
        string sessionId = null;

        if (channel != null)
        {
            sessionId = channel.SessionId;
        }
      [...]
 }
Vidicon answered 17/3, 2011 at 12:22 Comment(3)
This works only when the client shuts down gracefully. It doesn't work when the socket is just disconnected suddenly.Kedron
if the socket is disconnected, you should get a channel Fault event. The Closed event is raised when the client shuts down gracefully, the Faulted when it's unexpected (as in case of network failure).Vidicon
When you receive the Faulted event, you will also receive the Closed event after the Faulted event, so be sure to account for both and don't make the assumption, in your Closed event handler, that the connection ended cleanly.Bedlamite
A
-2
ICommunicationObject obj = (ICommunicationObject)callback;
                obj.Closed += new EventHandler((a, b) => 
                {
                    if (list.Exists(cli => cli.CallbackService == (ITecnobelRemoteServiceCallback)a))
                    {
                        var query = (from cc in list where cc.CallbackService == (ITecnobelRemoteServiceCallback)a select cc).ToList();
                        query.ForEach(
                            delegate (Client ccc)
                            {
                                ccc.CallbackService = null;
                            });
                    }

                });
Amann answered 3/7, 2015 at 15:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.