简体   繁体   中英

Check client status before calling callback method in WCF

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?

确保将所有属性超时设置为自动删除不活动的客户端,然后在try catch块中捕获超时异常并将其从字典中删除。

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));
        }
    }

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