【问题标题】:Can I have simultaneous streams on one physical file我可以在一个物理文件上同时流式传输吗
【发布时间】:2011-02-25 13:34:08
【问题描述】:

我有一个允许客户端下载一些文件的 wcf 服务。虽然每个客户端的请求都有一个新的服务实例,但如果两个客户端尝试同时下载同一个文件,第一个到达的请求会锁定文件,直到它完成。所以另一个客户端实际上是在等待第一个客户端完成,因为没有多个服务。必须有办法避免这种情况。

有没有人知道如何在服务器硬盘上没有多个文件的情况下避免这种情况?还是我做错了什么?

这是服务器端代码:

`public Stream DownloadFile(字符串路径) { System.IO.FileInfo fileInfo = new System.IO.FileInfo(path);

        // check if exists
        if (!fileInfo.Exists) throw new FileNotFoundException();

        // open stream
        System.IO.FileStream stream = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read);

        // return result
        return stream;
    }`

这是客户端代码:

public void Download(string serverPath, string path)
    {
        Stream stream;
        try
        {
            if (System.IO.File.Exists(path)) System.IO.File.Delete(path);
            serviceStreamed = new ServiceStreamedClient("NetTcpBinding_IServiceStreamed");
            SimpleResult<long> res = serviceStreamed.ReturnFileSize(serverPath);
            if (!res.Success)
            {
                throw new Exception("File not found: \n" + serverPath);
            }
            // get stream from server
            stream = serviceStreamed.DownloadFile(serverPath);

                // write server stream to disk
                using (System.IO.FileStream writeStream = new System.IO.FileStream(path, System.IO.FileMode.CreateNew, System.IO.FileAccess.Write))
                {
                    int chunkSize = 1 * 48 * 1024;
                    byte[] buffer = new byte[chunkSize];
                    OnTransferStart(new TransferStartArgs());
                    do
                    {
                        // read bytes from input stream
                        int bytesRead = stream.Read(buffer, 0, chunkSize);
                        if (bytesRead == 0) break;


                        // write bytes to output stream
                        writeStream.Write(buffer, 0, bytesRead);


                        // report progress from time to time
                        OnProgressChanged(new ProgressChangedArgs(writeStream.Position));
                    } while (true);

                    writeStream.Close();
                    stream.Dispose();



                }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            if (serviceStreamed.State == System.ServiceModel.CommunicationState.Opened)
            {
                serviceStreamed.Close();
            }
            OnTransferFinished(new TransferFinishedArgs());
        }
    }

【问题讨论】:

  • 您如何读取/发送文件给客户端?请显示一些代码。
  • 对不起,这里是服务器端函数和客户端函数。

标签: wcf streaming


【解决方案1】:

我同意 Kjörling 先生的观点,如果没有看到您在做什么,就很难提供帮助。既然您只是从服务器下载文件,为什么要以 R/W 的形式打开它(导致锁定)。如果您以只读方式打开它,则它不会锁定。如果缺少我的建议,请不要修改,因为这只是我对问题的解释,没有很多信息。

【讨论】:

  • 我添加了代码。我不明白,我如何以只读方式打开它?我已将 FileAccess 设置为读取,这不是你在说什么吗?或者我还有什么需要做的吗?
  • 不一定是FileAccess导致锁定,查看FileShareFileStream可用的第三个参数。
【解决方案2】:

试试这个,应该可以让两个线程同时独立读取文件:

System.IO.FileStream stream = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);

【讨论】:

  • 好的,我会尝试使用 FileShare,只是我现在无法尝试。它必须等到我明天去我的办公室......然后我会检查答案,如果它有效。
猜你喜欢
  • 2020-02-15
  • 1970-01-01
  • 2011-12-08
  • 1970-01-01
  • 2019-09-21
  • 1970-01-01
  • 2015-05-05
  • 2016-08-07
  • 2015-06-20
相关资源
最近更新 更多