使用 WCF 流上传文件,从流中进行微小读取

发布于 2024-08-08 03:18:45 字数 767 浏览 6 评论 0原文

我已经使用WCF的流实现了文件上传。一切都按预期工作,但是我遇到了一个问题:我分配 4kb 缓冲区来从传入流中读取数据,但 WCF 仅读取 255 字节。这是我的上传函数:

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};
        }

在这一行仅读取 255 个字节: while ((count = inStream.Read(buffer, 0, buffer.Length)) > 0)。有什么我可以更改的设置,还是我做错了什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

§对你不离不弃 2024-08-15 03:18:45

如果可以的话请发布您的配置。配置应指定默认值或覆盖值,如下所示:

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

尝试此 MSDN 链接 该人提到他也遇到了同样的问题,只获取 255 字节,他标记了答案,似乎解决了他的问题。它指出:

“为了将流传递给 WCF 方法,Stream 参数必须是操作(或消息正文)中的唯一参数...”

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

始终不够 2024-08-15 03:18:45

我想你也遇到了和我一样的问题。我在这里解决了这个问题:通过 WCF 下载文件比通过 IIS 慢

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文