简体   繁体   English

进程无法访问文件 <filepath> 因为它正在被另一个进程使用

[英]The process cannot access the file <filepath> because it is being used by another process

I'm upload big files dividing its on chunks(small parts) on my ASMX webservice(asmx doesn't support streaming, I not found another way): 我在ASMX网络服务上上传了大文件,将其分成大块(小部分)(asmx不支持流式传输,我找不到另一种方式):

bool UploadChunk(byte[] bytes, string path, string md5)
{
      ...
      using (FileStream fs = new FileStream(tempPath, FileMode.Append) )
      {   
            fs.Write( bytes, 0, bytes.Length );
      }
      ...
      return status;
}

but on some files after ~20-50 invokes I catch this error: 但是在〜20-50调用之后的某些文件上,我捕获了此错误:
The process cannot access the file because it is being used by another process. 该进程无法访问该文件,因为该文件正在被另一个进程使用。

I suspect that this related with Windows can't realize the file. 我怀疑与Windows相关的文件无法实现。 Any idea to get rid of this boring error? 有什么办法摆脱这个无聊的错误吗?

EDIT 编辑
the requests executes sequentially and synchronously 请求顺序地和同步地执行



EDIT2 编辑2

client code looks like: 客户代码如下:

_service.StartUpload(path);

...
do
{
..
bool status = _service.UploadChunk(buf, path, md5);
if(!status)return Status.Failed;
..
}
while(bytesRead > 0);

_service.CheckFile(path, md5);

Each request is handled independently. 每个请求都是独立处理的。 The process still accessing the file may be the previous request. 仍在访问文件的过程可能是先前的请求。

In general, you should use file transfer protocols to transfer files. 通常,您应该使用文件传输协议来传输文件。 ASMX is not good for that. ASMX对此不利。

And, I presume you have a good reason to not use WCF? 而且,我认为您有充分的理由不使用WCF?

Use WhoLockMe at the moment the error occurs to check who is using the file. 发生错误时,请使用WhoLockMe来检查谁在使用该文件。 You could put the application into debug mode and hold the break point to do this. 您可以将应用程序置于调试模式并按住断点来执行此操作。 In all probability it will be your process. 这很可能是您的过程。

Also try adding a delay after each transfer (and before the next) to see if it helps. 另外,请尝试在每次传输之后(以及下一次传输之前)添加延迟,以查看是否有帮助。 Maybe your transfers are too fast and the stream is still in use or being flushed when the next transfer comes in. 也许您的传输速度太快,下一次传输进来时,流仍在使用中或被冲洗。

use this for creating a file 用它来创建文件

if you want to append something then add FileMode.Append 如果您想附加一些内容,请添加FileMode.Append

var filestreama = new FileStream(name, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);

Option 1: Get the requirements changed so you don't have to do this using ASMX. 选项1:更改需求,因此您不必使用ASMX进行更改。 WCF supports a streaming model that I'm about to experiment with, but it should be much more effective for what you want. WCF支持我将要试验的流模型,但是对于您想要的模型,它应该更加有效。

Option 2: Look into WSE 3.0. 选项2:研究WSE 3.0。 I haven't looked at it much, but I think it extends ASMX web services to support things like DIME and MTOM which are designed for transferring files so that may help. 我没看太多,但是我认为它扩展了ASMX Web服务以支持DIME和MTOM之类的东西,这些东西旨在传输文件,因此可能会有所帮助。

Option 3: Set the system up so that each call writes a piece of the file into a different filename, then write code to rejoin everything at the end. 选项3:设置系统,以便每个调用将文件的一部分写入不同的文件名,然后编写代码以重新加入所有内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 下载文件时出现错误:该进程无法访问文件&#39;[filepath]&#39;,因为它正在被另一个进程使用 - Getting Error while downloading file: The process cannot access the file '[filepath]' because it is being used by another process “进程无法访问该文件,因为它正被另一个进程使用”与图像 - “The process cannot access the file because it is being used by another process” with Images 该进程无法访问文件“ XXX”,因为它正在被另一个进程使用 - The process cannot access the file 'XXX' because it is being used by another process 该进程无法访问文件XXX,因为它正在被另一个进程使用 - The process cannot access the file XXX because it is being used by another process SSIS - 该进程无法访问该文件,因为它正被另一个进程使用 - SSIS - The process cannot access the file because it is being used by another process DownloadFile该进程无法访问文件,因为该文件正在被另一个进程使用 - DownloadFile The process cannot access the file because it is being used by another process 该进程无法访问该文件,因为它正在被另一个进程使用 - The process cannot access the file because it is being used by another process 该进程无法访问该文件,因为它正在被另一个进程使用 - The process cannot access the file because it is being used by another process 该进程无法访问文件“路径”,因为它正在被另一个进程使用 - The process cannot access the file 'path' because it is being used by another process “进程无法访问该文件,因为它正由另一个进程使用” - “The process cannot access the file because it is being used by another process ”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM