1

I have a Server Application who EXEC another app, that second app builds a log file, and I want the server to send it to a client via WCF.

I want the client to read the log while the EXEC app update it.

I've used an operation contract that returns stream (the log file). the problem is that when I first send the stream, the log is still empty, and the client don't see the "update" that the server EXEC writes.

here is the server code:

    public Stream GetLog()
    {
        Stream stream = new FileStream(@"Log.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
        return stream;
    }

the EXEC from the server is simply writing to the "Log.txt" file

here is the client code:

        public void SaveLog()
    {
        Stream stream = ServerProxy.GetLog();
        StreamWriter writer = new StreamWriter("NEWLog.txt");
        writer.AutoFlush = true;
        StreamReader reader = new StreamReader(stream);
        char[] buf = new char[100];
        int count = 0;
        while (count = reader.ReadBlock(buf, 0, 100) > 0)
        {
            writer.Write(buf, 0, count);
        }
        stream.Close();
        writer.Close();
    }

how can I update the Client with the Log in REAL-TIME?

IsraelKo
  • 106
  • 1
  • 5

2 Answers2

1

I think that your bindings are wrong. you should set your transferMode="Streamed"

there you can find an example:

http://msdn.microsoft.com/en-us/library/ms751463.aspx

giammin
  • 18,620
  • 8
  • 71
  • 89
0

There are a couple of possibilities. Are you allowed to modify the contract? If you can use a session aware binding like netTcpBinding, you could modify your contract to use a callback contract where the callback is used to publish new log data.

[ServiceContract]
public interface ILogCallback
{
    [OperationContract(IsOneWay = true)]
    void NewLog(string logString);
}

[ServiceContract(CallbackContract = typeof(ILogCallback))]
public interface ILog
{
    /// Call this method to sign up for future log messages, 
    /// which will be sent to the callback NewLog.
    [OperationContract]
    void SignMeUpForLogs();
}

What kind of bindings can you use? Are sessions a viable alternative or must you use PerCall?

Regards, Fredrik

Fredrik
  • 91
  • 3
  • I'm using NetTcpBinding, and your solution is really solves the problem, but the implementation is quit complicated to store all the subscribers wants the log, and operate thread to catch the changes in the log file and update all the subscribers. I'll be happy if my simple code will work. – IsraelKo Feb 10 '13 at 20:42