1

我设法创建了一个带有回调的 WCF 服务。回调按预期工作,但仅适用于一个客户端。如果我启动另一个客户端,第一个客户端停止接收回调,但第二个客户端接收它们两次,依此类推。我已经尝试InstanceContextMode过 Single、PerCall 和 PerSession 模式,但它会导致同样的问题。

你知道如何解决这个问题吗?

这是服务类:

[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
    }

这是回调类:

[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
    }
4

1 回答 1

1

我觉得静态存在问题,因为您将回调引用存储在静态中。回调参考包含与它回调的客户端相关的信息。

这导致第二个客户注册时第一个客户错过了它。

更多信息:具有多个客户端的 WCF 回调

于 2014-06-24T06:37:23.690 回答