我设法创建了一个带有回调的 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
}