Unable to get data from a WCF client
I am developing a DLL that will provide synchronized time stamps to multiple applications running on the same machine. The timestamps are altered in a thread that uses a high-performance timer and a scalar to provide the appearance of moving faster than real-time. For obvious reasons I want only 1 instance of this time library, and I thought I could use WCF for the other processes to connect to this and poll for timestamps whenever they want. When I connect however I never get a valid timestamp, just an empty DateTime. I should point out that the library does work. The original implementation was a single DLL that each application incorporated and each one was synced using windows messages. I'm fairly sure it has something to do with how I'm setting up the WCF stuff, to which I am still pretty new. Here are the contract definitions:
public interface ITimerCallbacks{
[OperationContract(IsOneWay = true)]
void TimerElapsed(String id);
}
[ServiceContract(Sessio开发者_C百科nMode = SessionMode.Required, CallbackContract = typeof(ITimerCallbacks))]
public interface ISimTime
{
[OperationContract]
DateTime GetTime();
}
Here is my class definition:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class SimTimeServer: ISimTime
The host setup:
// 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();
and the implementation of the interface function server-side:
public DateTime GetTime()
{
if (ThreadMutex.WaitOne(20))
{
RetTime = CurrentTime;
ThreadMutex.ReleaseMutex();
}
return RetTime;
}
Lastly the client-side implementation:
Callbacks myCallbacks = new Callbacks();
DuplexChannelFactory<ISimTime> pipeFactory =
new DuplexChannelFactory<ISimTime>(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;
}
I haven't even gotten to the point where I would need the callback. I plan to mimic the windows timer functionality to provide timers that can work with the sped up time stamps. That is where the callback would be used.
The GetTime method is a method to get the current time (think of it like the DateTime.Now method to get the current system time).
精彩评论