Unable to get data from a WCF client

Posted by Scott on Stack Overflow See other posts from Stack Overflow or by Scott
Published on 2010-04-20T17:31:39Z Indexed on 2010/04/20 17:33 UTC
Read the original article Hit count: 238

Filed under:
|

I am developing a DLL that will provide sychronized 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 time stamp, 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(SessionMode = 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 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;
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about wcf