Uploading file with WCF streaming, tiny reads from stream

夙愿已清 提交于 2019-12-14 03:57:21

问题


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?


回答1:


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)..."




回答2:


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



来源:https://stackoverflow.com/questions/1594510/uploading-file-with-wcf-streaming-tiny-reads-from-stream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!