1

I'm trying to let my service knows when one of the clients is disconnected.

I'm using wsDualHttpBinding.

Currently, I'm tried to use this event :

OperationContext.Current.Channel.Closed += new EventHandler((sender, e) => ClientIsDisconnected(sender, e, currentCallbackChannel));

But this event is never fired...

Please help me to know how it'd be done !

Edit :

Thanks to anderhil, I finally replaced wsDualHttpBinding by netTcpBinding (with the appropriate configuration described here : http://msdn.microsoft.com/en-us/library/ff647180.aspx#Step1).

With netTcpBinding, the Closed event fires without any problem... Still don't know why but it works.

Oliboy50
  • 2,661
  • 3
  • 27
  • 36
  • so you haven't received the event even once? How do you test it? Where do you use your code?(in what operation) Why do you need duplex? – Andriy Kizym Jun 04 '13 at 17:04
  • Nope I haven't received it yet. I test it by quitting one of my client WinForm. I attach the EventHandler when one of my client is connecting to the service, just after that I have registered the current CallbackChannel to a Channel List. I need duplex to let each client know when an other client is disconnected. – Oliboy50 Jun 04 '13 at 17:13
  • I edited my Question, with more code. – Oliboy50 Jun 04 '13 at 17:24
  • Add some button to WinForm, and when button is clicked take the client channel and call .Close() or .Abort() method and then try to test it by clicking the button. Also uncomment the Faulted event - these two events always should go together in such cases. – Andriy Kizym Jun 04 '13 at 17:28
  • 1
    Also, it is interesting why these events are not called, but overall, this method of handling events doesn't cover cases when your client machine will be powered off, or client router will turn off, or smth like this, cause client should send some die message to let service know about client is turning off. So in your case i'd do some timer which "asks" all clients whether they are alive. – Andriy Kizym Jun 04 '13 at 17:30
  • If I understood, I shouldn't .close() my service connection immediately after that I received what I wanted ? And when I will quit the client application, the connection will be closed itself ? I think I misunderstood something... – Oliboy50 Jun 04 '13 at 17:33
  • 1
    let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/31200/discussion-between-anderhil-and-oliboy50) – Andriy Kizym Jun 04 '13 at 17:34

2 Answers2

1

The Closed event should occur on a graceful disconnect; is that what's happening? To detect the pure socket disconnect, listen for the Faulted event:

OperationContext.Current.Channel.Faulted += new EventHandler(FaultedHandler);
Geoff
  • 8,551
  • 1
  • 43
  • 50
  • In fact, i tried both... How can i be sure if the event is fired ? I put a Console.WriteLine("blabla") in the function ClientIsDisconnected. But i'm not even sure if i can see it in the Service Output... – Oliboy50 Jun 04 '13 at 16:09
  • 1
    To debug the service, attach to it; good instructions are [here](http://stackoverflow.com/a/4710758/55487) – Geoff Jun 04 '13 at 16:11
  • Hum... it seems that my service is already attached.. Can you give me an example about how i should use this Event, maybe i do it wrong. – Oliboy50 Jun 04 '13 at 16:22
  • The EventHandler should be attached before or after that I register the `OperationContext.Current.GetCallbackChannel()` into my channel list ? – Oliboy50 Jun 04 '13 at 17:03
1

The issue you are having is likely becuase of WsDualHttpBinding. In case you have this binding, two connections are created, from client to service and from service to client.

When the application is deployed over the internet it can create some issues with supporting such applications, you need to be sure that people are not behind the firewall or NAT or etc that can prevent your service to connect back to client.

I still don't know why it doesn't work on local machine when testing, but i will try to resolve it and update the answer.

As you told me more details in our chat, from the nature of your application it's better to use NetTcpBinding. In this case it's easier to understand what is happening cause one connection is created, and you will receive the notifications in case of gracefull close or abort of client.

As i told you before, anyway it's better to create some heartbeat mechanism to have things more reliable in case of unexpected computer or router shutdown.

Also, you can find this good cheat sheet on how to select communication between parties that involve WCF:

wcf binding cheat sheet

Andriy Kizym
  • 1,756
  • 2
  • 14
  • 29