1

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
    }
Stefan Schmid
  • 1,012
  • 10
  • 28
  • 2
    I feel that there is some problem with static. You store reference in Static for callback so.http://stackoverflow.com/questions/3152703/wcf-callback-with-multiple-clients – dotnetstep Jun 24 '14 at 06:00
  • Thanks, those links look really helpful. By the way this code was from a tutorial found here: [A Simple WCF Service Callback Example](http://adamprescott.net/2012/08/15/a-simple-wcf-service-callback-example/) **Edit:** I don't know if I need that Publish/Subscribe model as I only need to "subscribe" (add callback reference) and then read some data from the DB and send it to the corresponding client. – Stefan Schmid Jun 24 '14 at 06:08
  • @dotnetstep Please make your comment an answer. Removing `static` fixed the problem. – Stefan Schmid Jun 24 '14 at 06:20

1 Answers1

1

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

Community
  • 1
  • 1
dotnetstep
  • 17,065
  • 5
  • 54
  • 72