none
c# WCF client server and disconnection RRS feed

  • Question

  • I created simple client server with WCP named pipes.

    deliberately kill the client to see if on the server I get dissconection event.

    from reading I should use :

    private ServiceHost host;
            public StartListener() { }
            public void start()
            {
                host = new ServiceHost(
           typeof(StringReverser),
           new Uri[]{
              new Uri("net.pipe://192.168.10.10")
           });
    
                host.AddServiceEndpoint(typeof(IStringReverser),
                  new NetNamedPipeBinding(),
                  "PipeReverse");
                host.Faulted += new EventHandler(Channel_Faulted);
                host.Closed += new EventHandler(Channel_Faulted);
                            
                host.Open();
                
    
            }
            void Channel_Faulted(object sender, EventArgs e)
            {
                Logout((IContextChannel)sender);
            }
            protected void Logout(IContextChannel channel)
            {
                string sessionId = null;
    
                if (channel != null)
                {
                    sessionId = channel.SessionId;
                }
            }

    but no event fired on Faulted & Closed

    why?

    Thursday, June 6, 2019 11:06 AM

All replies

  • IMO, you kill the client or the client throws an exception, the connection to the service is lost, and the service is not going to respond or do anything. 

    It's a request and response scenario. 

    I think the service would need to be doing some response back to the client for the event to be raised.

    https://en.wikipedia.org/wiki/Request–response

    Anyway, WCF issues can be discussed at the WCF forum.

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=wcf

     
    Thursday, June 6, 2019 12:09 PM
  • Closed only gets called if the client is closed by calling the Close (or equivalent) method. It is important to note that faults will not close the connection. You still have to explicitly close it. So a disconnected client won't raise any events in your code. The next time you try to use that host it'll fault but it doesn't do it automatically. As with most things in .NET disconnects aren't detected until you try to use the object again.

    The only way I know to "detect" a disconnect in WCF is to have a ping-like method exposed by the service that the client can periodically call. When the client tries to ping and the remote server is gone then it'll fault.


    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, June 6, 2019 2:04 PM
    Moderator
  • ok thanks
    Thursday, June 6, 2019 10:08 PM