Search code examples
c#wcfwcf-callbacks

WCF Callbacks not working for multiple clients


I've managed to create a WCF service with callbacks. The callbacks are working as expected, but only for one client. If I start another client, the first one stops receiving callbacks, but the second one receives them twice and so on. I've tried InstanceContextMode in Single, PerCall and PerSession Mode but it leads to the same problem.

Do you know how to fix this problem?

Here's the service class:

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant, InstanceContextMode = InstanceContextMode.Single)]
    public class HostFunctions : IHostFunctions
    {
        #region Implementation of IHostFunctions

        public static IHostFunctionsCallback Callback;
        public static Timer Timer;

        public void OpenSession()
        {
            Console.WriteLine("> Session opened at {0}", DateTime.Now);
            Callback = OperationContext.Current.GetCallbackChannel<IHostFunctionsCallback>();
            Timer = new Timer(1000);
            Timer.Elapsed += OnTimerElapsed;
            Timer.Enabled = true;
        }

        private void OnTimerElapsed(object sender, ElapsedEventArgs e)
        {
            if (Callback != null)
            {
                Callback.OnCallback();
            }
        }

        #endregion
    }

Here's the callback class:

[CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant, UseSynchronizationContext = false)]
    public class Callback : IHostFunctionsCallback
    {
        #region Implementation of ICallback

        public void OnCallback()
        {
            Console.WriteLine("> Received callback at {0}", DateTime.Now);
        }

        #endregion
    }

Solution

  • I feel that there is proble with static as you store reference for callback in static. Callback reference contain information related to client that it callback.

    That resulted that first client miss it when second client registered.

    More Info : WCF callback with multiple clients