0

I have a WCF Service and WCF Client working on Duplex Channel using netTCPBinding.

I store connected users in a dictionary ( Dictionary<int userID,CallbackInstance instance> )

When user Disconnect regularly, call Disconnect from service and I remove the user from my connected user list. It works fine.

but when client pc disconnects unregularly, client could not call Disconnect method, so client still in connected user list, that's the problem. Because when my WCF server check server for online users for a callback, server try to call client's callback method, but client is not available, and my WCF Server App crash.

Is it possible to check client status before calling callback instance?

2 Answers2

0

Make sure all the properties timeouts are set to automatically remove inactive clients then catch the timeout exception in a try catch block and remove it from your dictionary.

Wobbles
  • 3,033
  • 1
  • 25
  • 51
0

I fix this with:

1.Method to ping from client to server to keep the conection active for each 30 seconds.

2.On server binding, ReceiveTimeout with 1 minute.

3.Foreach callback created a IcommunicationObject, using the Closed event to remove the inactive client.

//Adding a client callback 

OperationContext context = OperationContext.Current;
ICallback callback = context.GetCallbackChannel(); 
ICommunicationObject obj = (ICommunicationObject)callback; 
obj.Closed += new EventHandler(obj_Closed);



//Event for inactive clients
 void obj_Closed(object sender, EventArgs e)
    {
        if (_callbacks.ContainsValue(((ITecnobelRemoteServiceCallback)sender)))
        {
            var item = _callbacks.First(kvp => kvp.Value == ((ITecnobelRemoteServiceCallback)sender));
            _callbacks.Remove(item.Key);
            treeViewClients.Nodes.RemoveByKey(item.Key.Id);
            treeViewClients.Refresh();
            _registeredUsers--;
            listBoxStatus.Items.Add(String.Format("Usuário {0} estava inativo e foi removido", item.Key.Id));
        }
    }