Reputation: 13933
First of all, I made my problem as simple as I could to show you that, in simple words, when I transfare a stream object in wcf, the method 'Read' dont care about any parameters I give him.
I have WCF client & service, TransferMode = Streamed on both sides. basicHttpBinding. The client send to the service a Stream object and the service read the stream and write it on a new file that he create. The client send a subclass of stream that I created : FileStreamEx that inherit from FileStream.
I puted this class in that namespace so I can watch everytime the service call 'Read' method.
namespace ConsoleApplication1
{
/// <summary>
/// The only thing I gain from this class is to know how much was read after every time I call "Read" method
/// </summary>
public class FileStreamEx : FileStream
{
/// <summary>
/// how much was read until now
/// </summary>
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;
Console.WriteLine("Read: " + _ReadUntilNow);//Show how much was read until now
return ReturnV;
}
}
public class Program
{
static void Main(string[] args)
{
ServiceReference1.IJob Service1 = new ServiceReference1.JobClient();
string FileName = "Chat.rar";
string Path = @"C:\Uploadfiles\";
FileStreamEx TheStream = new FileStreamEx(Path + FileName, FileMode.Open, FileAccess.Read, FileShare.None);
Service1.UselessMethod1(TheStream);
}
}
}
Until now there is nothing complicated. Below there is the UselessMethod1 method code. But befor that, The problem allready started: After I call Service1.UselessMethod1(TheStream), Insted of going to the UselessMethod1 method, the 'Read' method begin 3 times, no matter what the size of the file and the out put is (Only on the client side):
Read: 256
Read: 4352
Read: 69888
And only after the 'Read' method gets called 3 times, the UselessMethod1 Begin. If you will look at the code below, You will see that My buffer arr size is only 1024 for each Read! By logic, after MyStream.Read(buffer, 0, bufferSize)), My 'Read' method need to begin but this is not the case, the 'Read' method start randomaly (or not, I cant understand it) and when my 'Read' method begin, the Length of the Arr of the parameter 'byte[] array' that my 'Read' method have is never 1024 when it gets called.
public void UselessMethod1(Stream MyStream)
{
using (FileStream fs = new FileStream(@"C:\Upload\"+"Chat.rar", FileMode.Create))
{
int bufferSize = 1024 ;
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;
}
}
}
Some of the out put in this code is: (Sory I didnt understand how to show image) http://desmond.imageshack.us/Himg36/scaled.php?server=36&filename=prlbem.jpg&res=landing
But in my logic, the out put of this code need to be from the start:
Read: 1024
Read: 1024
Read: 1024
Read: 1024
Read: 1024
Read: 1024
...
..
.
But its not the out out! Where is my mistake? Is there even any miskate?
Upvotes: 2
Views: 647
Reputation: 87238
The server and the client may (and will likely) use different buffer sizes when reading the data. There's nothing wrong per se with your code.
If you want to force the client to use a smaller buffer, you can consider making the binding's transfer mode Streamed
, and use a small value for the MaxBufferSize
property of the binding (if you're using a custom binding, those properties will likely be on the transport binding element).
Upvotes: 1