无法从WCF客户端获取数据
问题描述:
我正在开发一个DLL,它将为在同一台计算机上运行的多个应用程序提供同步时间戳。在使用高性能计时器和标量的线程中更改时间戳,以提供比实时更快的移动外观。出于显而易见的原因,我只想要这个时间库的1个实例,我想我可以使用WCF为其他进程连接到它并轮询时间戳,只要他们想要。当我连接但是我从来没有得到一个有效的时间戳,只是一个空的DateTime。我应该指出,图书馆确实有效。原始实现是一个单独的DLL,每个应用程序都包含在一起,并且每个DLL都使用Windows消息进行同步。我相当确定它与我如何设置WCF的东西有关,我仍然很新。这里有合同的定义:无法从WCF客户端获取数据
public interface ITimerCallbacks { [OperationContract(IsOneWay = true)] void TimerElapsed(String id); } [ServiceContract(SessionMode = SessionMode.Required, CallbackContract = typeof(ITimerCallbacks))] public interface ISimTime { [OperationContract] DateTime GetTime(); }
这里是我的类定义:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)] public class SimTimeServer: ISimTime
的主机设置:
// set up WCF interprocess comms host = new ServiceHost(typeof(SimTimeServer), new Uri[] { new Uri("net.pipe://localhost") }); host.AddServiceEndpoint(typeof(ISimTime), new NetNamedPipeBinding(), "SimTime"); host.Open();
和接口功能服务器端的实现:
public DateTime GetTime() { if (ThreadMutex.WaitOne(20)) { RetTime = CurrentTime; ThreadMutex.ReleaseMutex(); } return RetTime; }
最后客户端执行:
Callbacks myCallbacks = new Callbacks(); DuplexChannelFactory pipeFactory = new DuplexChannelFactory(myCallbacks, new NetNamedPipeBinding(), new EndpointAddress("net.pipe://localhost/SimTime")); ISimTime pipeProxy = pipeFactory.CreateChannel(); while (true) { string str = Console.ReadLine(); if (str.ToLower().Contains("get")) Console.WriteLine(pipeProxy.GetTime().ToString()); else if (str.ToLower().Contains("exit")) break; }
答
我甚至没有到达需要回调的地步。我计划模仿Windows定时器功能来提供可以使用加速时间戳的定时器。这是回调将被使用的地方。
GetTime方法是一种获取当前时间的方法(想象它像DateTime.Now方法来获取当前系统时间)。