Last post Dec 12, 2008 05:40 AM by fafar
Dec 10, 2008 08:30 PM|fafar|LINK
I created a wcf service for uploading large files. I followed a very useful example found at
My service works well except for the performance. It takes a long time to upload a file.
In the implementation of the upload operation contract I have something like this:
binaryContent is a stream that is sent from the client.
byte buffer = new byte;
int read = 0;
while ((read = binaryContent.Read(buffer, 0, buffer.Length)) != 0)
/* Here I write a chunk of data to database */
The issue I am having is that I set maxBufferSize in the
basicHttpBinding both on client and the service side to 1MB (1048576) so I did with the lenght of the buffer here in the above code, but it seems the maxBufferSize value has nothing to do with the length of data chunk read in each loop here since
binaryContent.Read reads always 4096 bytes causing data corruption at the end.
I could set the buffer size back to 4096 but I would like to know if it is possible to read more bytes than 4KB block at a time.
Thanks in advance,
Dec 11, 2008 12:44 AM|johnwsaunders3|LINK
I don't k
I don't know if it's possible to read more, but you should check with Fiddler or something to see if the server is
sending more than 4k at a time.
Dec 12, 2008 05:40 AM|fafar|LINK
Thanks for the answer. I found that 4KB is a default buffer size for reading a stream with Read method. The default buffer size can be defined when the stream object is constructed, but in this case the uploadFile
control does not give me any option to set a default buffer size... Of course it can be done using some sort of conversion of the stream after the file is uploaded... but I actually come up with a workaround solution to my problem:
For now, I use help from a temp 1MB buffer (an additional byte array) that collects all the 4KB blocks until it is full, and then I flush that temp buffer to
database. This little adjustment improves performance significantly even if it is a “quick and dirty” solution...