IsraelKo
IsraelKo

Reputation: 116

streaming a file while hes being written in WCF

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?

Upvotes: 1

Views: 473

Answers (2)

Fredrik
Fredrik

Reputation: 101

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

Upvotes: 0

giammin
giammin

Reputation: 18958

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

Upvotes: 1

Related Questions