Im developing in WCF and inside my client code I creat filestream with a file to transfer to the service (by sending it as stream in message). After the service got the stream, he read it peace by peace and write it on new file; Service download the file from the client to him, thats all.
I override the method in filestream, Read, so I can know how much was readed in the client side, when the service is downloading in his side.
public class FileStreamEx : FileStream
{
public long _ReadUntilNow { get; private set; }
public FileStreamEx(string path, FileMode mode, FileAccess access, FileShare share)
: base(path, mode, access, share)
{
this._ReadUntilNow = 0;
}
public override int Read(byte[] array, int offset, int count)
{
int ReturnV = base.Read(array, offset, count);
_ReadUntilNow += ReturnV;
return ReturnV;
}
}
My goal is to know from the client side, how much was readed by the service every second.
This can be implemented by seeing the _ReadUntilNow value simply from the client becouse the service only use a reference to the FileStreamEx object.
My problem is that befor the service even start read the stream I gave him, the _ReadUntilNow value = size of the file. Only way for this to heppend is calling my overrided method, Read, befor I do.
My question is who call the Read befor me, why and what can I do to prevent it?
My client:
public static void CallService()
{
ServiceReference1.IJob Service1 = new ServiceReference1.JobClient(new InstanceContext(new CCBImplement()));
DLL.FileStreamEx TheStream TheStream = new DLL.FileStreamEx(@"C:\Uploadfiles\Chat.rar", FileMode.Open, FileAccess.Read, FileShare.None);
ServiceReference1.RemoteFile RemoteFile1=new ServiceReference1.RemoteFile("Chat.rar", TheStream.Length, @"C:\Uploadfiles\Chat.rar", TheStream);
Service1.UselessMethod1(RemoteFile1);
new Thread(new ThreadStart(Check_Only_ReadUntilNow_Every_Second)).Start();
}
Service Code:
public void UselessMethod1(RemoteFile RemoteFile)
{
Stream MyStream = RemoteFile.FileByteStream;
using (FileStream fs = new FileStream(@"C:\Upload\"+RemoteFile.FileName, FileMode.Create))
{
int bufferSize = 1 ; // 1 MB buffer
byte[] buffer = new byte[bufferSize];
int totalBytes = 0;
int bytes = 0;
while ((bytes = MyStream.Read(buffer, 0, bufferSize)) > 0)
{
fs.Write(buffer, 0, bytes);
fs.Flush();
totalBytes += bytes;
}
}
}
RemoteFile class:
[MessageContract]
public class RemoteFile : IDisposable
{
[MessageHeader]
public string FileName;
[MessageHeader]
public string Path;
[MessageHeader]
public long Length;
[MessageBodyMember]
public Stream FileByteStream;
public RemoteFile() { }
public RemoteFile(string FileName, string Path, long Length, Stream FileByteStream)
{
this.Path = Path;
this.FileName = FileName;
this.Length = Length;
this.FileByteStream = FileByteStream;
}
public void Dispose()
{
if (FileByteStream != null)
{
FileByteStream.Close();
FileByteStream = null;
}
}
}
I think that his question is that:
on side A, there is a service host with a method that read files on stream mode.
on side B, there is a client that sends a file using this service.
he expanded the FileStream calss to add a variable which holds the amount of bytes read from the file.
he calls the service on side A, but before it even reaches the method- the extra variable holds the size of the whole file..which means the whole file was read.
the question is why.