andrew_m
andrew_m

Reputation: 171

Uploading file with WCF streaming, tiny reads from stream

I've implemented file uploading using WCF's streaming. Everything works as expected, however i faced one issue: i'm allocating 4kb buffer to read from incoming stream, but WCF reads only 255 bytes. Here is my upload function:

public UploadResponse UploadFile(FileDto fileDto)
        {
            using (var inStream = fileDto.FileStream)
            using (var outStream = new FileStream("OutFile.txt", FileMode.Create))
            {
                var buffer = new byte[4096];
                int count;
                while ((count = inStream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    outStream.Write(buffer, 0, count);
                }
            }
            return new UploadResponse {DocumentId = -1};
        }

Only 255 bytes reading at this line: while ((count = inStream.Read(buffer, 0, buffer.Length)) > 0). Is there any setting i can change, or am i doing something wrong?

Upvotes: 2

Views: 4235

Answers (2)

Greg Smalter
Greg Smalter

Reputation: 6699

I think you had the same problem I did. I solved it here: File download through WCF slower than through IIS

Upvotes: 1

Tanner
Tanner

Reputation: 22733

Post your configs if you can please. The config should specify the defaults or overriden values, something like below:

    <binding name="FileTransferServicesBinding"
    maxReceivedMessageSize="1048576" messageEncoding="Mtom">
      <readerQuotas maxArrayLength="1048576" maxBytesPerRead="1048576"
    maxNameTableCharCount="1048576" maxStringContentLength="1048576"> </readerQuotas>
    </binding>

Try this MSDN Link the guy mentions that he had the same issue with only getting 255 bytes, he has an answer marked and it seems to resolve his issue. It states:

"In order to pass a stream to a WCF method, the Stream parameter must be the only parameter in the operation (or in the message body)..."

Upvotes: 1

Related Questions