Wcf 我可以在一个物理文件上同时使用流吗

Wcf 我可以在一个物理文件上同时使用流吗,wcf,streaming,Wcf,Streaming,我有一个wcf服务,允许客户端下载一些文件。尽管每个客户机的请求都有一个新的服务实例,但如果两个客户机同时尝试下载同一个文件,则到达的第一个请求将锁定该文件,直到完成该文件。所以另一个客户端实际上在等待第一个客户端完成,因为没有多个服务。必须有办法避免这种情况 有没有人知道我如何避免在硬盘服务器上没有多个文件的情况下发生这种情况?还是我做错了什么 这是服务器端代码: public void Download(string serverPath, string path) {

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

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

这是服务器端代码:

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());
        }
    }
`公共流下载文件字符串路径 { System.IO.FileInfo FileInfo=new System.IO.FileInfopath

        // 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());
        }
    }

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

尝试此操作,它应允许两个线程同时独立地读取文件:

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

您是如何读取/发送文件到客户端的?请显示一些代码。对不起,这是服务器端函数和客户端函数。我添加了代码。我不明白,我是如何以只读方式打开它的?我已经将FileAccess设置为Read,这不是您所说的吗?或者我还需要做其他事情吗?这不一定是FileAccess ca使用锁,签出FileShare,这是FileStream可用的第三个参数。好的,我将尝试使用FileShare,只是我现在无法尝试。它将不得不等到明天我去办公室…如果它有效,我将检查答案。