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

发布于 2024-10-14 03:37:08 字数 751 浏览 5 评论 0原文

我在我的 ASMX Web 服务上上传大文件,将其分成块(小部分)(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;
}

但在约 20-50 次调用后的某些文件上,我发现了此错误:
该进程无法访问该文件,因为该文件正在被另一个进程使用。

我怀疑这与Windows无法实现该文件有关。有什么办法可以摆脱这个无聊的错误吗?

编辑
请求按顺序同步执行



编辑2

客户端代码如下:

_service.StartUpload(path);

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

_service.CheckFile(path, md5);

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):

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:
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. Any idea to get rid of this boring error?

EDIT

the requests executes sequentially and synchronously

EDIT2

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

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

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

发布评论

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

评论(4

嘦怹 2024-10-21 03:37:08

每个请求都是独立处理的。仍在访问该文件的进程可能是先前的请求。

一般来说,您应该使用文件传输协议来传输文件。 ASMX 对此并不好。

而且,我认为您有充分的理由不使用 WCF?

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.

And, I presume you have a good reason to not use WCF?

长伴 2024-10-21 03:37:08

发生错误时使用 WhoLockMe 来检查谁正在使用该文件。您可以将应用程序置于调试模式并保持断点来执行此操作。很可能这将是您的过程。

还可以尝试在每次传输后(以及下一次传输之前)添加延迟,看看是否有帮助。也许您的传输速度太快,并且流仍在使用中或在下一次传输进入时被刷新。

Use WhoLockMe at the moment the error occurs to check who is using the file. 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.

动次打次papapa 2024-10-21 03:37:08

选项 1:更改要求,这样您就不必使用 ASMX 来执行此操作。 WCF 支持我即将尝试的流模型,但它对于您想要的东西应该更有效。

选项 2:研究 WSE 3.0。我没怎么看过它,但我认为它扩展了 ASMX Web 服务以支持 DIME 和 MTOM 等专为传输文件而设计的东西,因此可能会有所帮助。

选项 3:设置系统,以便每次调用都将文件的一部分写入不同的文件名,然后编写代码以在最后重新加入所有内容。

Option 1: Get the requirements changed so you don't have to do this using ASMX. WCF supports a streaming model that I'm about to experiment with, but it should be much more effective for what you want.

Option 2: Look into 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.

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.

无可置疑 2024-10-21 03:37:08

请使用它来创建文件,然后添加 FileMode.Append

如果您想附加某些内容,

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

use this for creating a file

if you want to append something then add FileMode.Append

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