How to determine if a client is alive for a callback in wcf?

I have a WCF implementation and I host it on a Windows Service (myself). I am using a contract inorder callback to trigger some client side events.
The question is how can I be sure or verify that the client is still alive to fire its callback event. Is there a verification mechanism? I am using .NET 3.5.
Thank.

+3


source to share


2 answers


There is no built-in way.

If the client is not available to handle the callback, then your service will either hang or throw an exception when it tries to call the client's callback (depending on the state of the callback channel).



One possible solution to this problem is here

0


source


My approach to the same problem was to create a "DefaultCallback" class that implements the callback interface and does nothing (it does not, of course, throw a Not ImplmentedException). Then you can write some code like this:

    private IServiceCallBack[] GetCallBack()
    {
        var returnValue = new IServiceCallBack[1];

        var com = (ICommunicationObject)(returnValue[0] = OperationContext.Current.GetCallbackChannel<IServiceCallBack>());

        com.Closing += new EventHandler((object sender, EventArgs e) =>
        {
            returnValue[0] = new DefaultCallBack();
        });

        com.Faulted += new EventHandler((object sender, EventArgs e) =>
        {
            returnValue[0] = new DefaultCallBack();
        });

        return returnValue;
    }

      



This way, whenever a callback client is in a closed or error state, it is replaced with a compatible object that does nothing.

0


source







All Articles