ASP。netmvc 2:“进程不能访问文件,因为它正在被另一个进程使用。”

时间:2022-12-20 21:03:26

I'm trying to use Controller.File to return a FilePathResult from a view in my ASP.NET MVC 2 project. I use it like this:

我在用控制器。从我的ASP中的视图返回文件路径结果的文件。净MVC 2项目。我这样使用它:

return File(pdfFilePath, "application/pdf", "foo.pdf");

However I keep getting this error in the yellow screen of death:

但是我在《死亡黄幕》中不断地犯这个错误:

The process cannot access the file [the file path] because it is being used by another process.

This error usually comes up when you forget to close a file stream, but I figured that this should be taken care of by the ASP.NET MVC framework. This doesn't happen every time, but rather periodically. Sometimes I get the file just fine but then it just stops working. I am using the development server when testing this.

当您忘记关闭文件流时,通常会出现这个错误,但是我认为这应该由ASP来处理。净MVC框架。这种情况并非每次都发生,而是周期性地发生。有时我得到的文件很好,但它只是停止工作。我正在使用开发服务器进行测试。

Any ideas?

什么好主意吗?

4 个解决方案

#1


2  

Are you accessing the file prior to the line of code you provided? If so, how are you accessing it?

您是否在提供的代码行之前访问文件?如果是,您如何访问它?

When accessing files, try to use the following to avoid file stream conflicts:

在访问文件时,尝试使用以下方法来避免文件流冲突:

File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

The last enum FileShare.ReadWrite will allow other file streams to read and write to the file even if you have it open. Of course, it is better to remember to close your stream ASAP.

最后一个枚举文件共享。ReadWrite将允许其他文件流对该文件进行读写,即使您打开了该文件。当然,最好记住尽快关闭流。

http://msdn.microsoft.com/en-us/library/y973b725.aspx

http://msdn.microsoft.com/en-us/library/y973b725.aspx

#2


0  

Do you have the file already open when you get this message?

收到此消息时,文件是否已经打开?

If you do it might be Adobe locking the file.

如果您这样做,可能是Adobe锁定文件。

#3


0  

The likeliest scenario is that something other than ASP.NET / IIS has the file open. Have you ensured that no other processes have a lock on the file when this error occurs?

最有可能的情况是ASP之外的东西。NET / IIS打开了文件。当这个错误发生时,您是否确保其他进程没有对文件进行锁定?

If you have access to the server when the error happens, you can use a tool like Process Explorer to view what exactly is locking the file.

如果发生错误时您可以访问服务器,您可以使用Process Explorer这样的工具来查看锁定文件的确切内容。

#4


0  

The locking can occur from within asp.net itself - as asp.net displays thread-agility, and as such it may be another thread from within the asp.net threadpool completing the request. This would be why you only see this issue intermittently. Baddie's answer is basically the solution to your problem. As an aside, you might find other issues if you're using resources that contain state that are declared as threadstatic. If this is the case you might want to look at making use of CallContext.

这种锁定可以在asp.net内部进行,因为asp.net显示了线程敏捷性,因此它可能是asp.net threadpool中完成请求的另一个线程。这就是为什么您只能断断续续地看到这个问题。巴德迪的答案基本上就是解决你的问题的办法。顺便提一下,如果您使用的资源包含声明为threadstatic的状态,您可能会发现其他问题。如果是这种情况,您可能需要考虑使用CallContext。

#1


2  

Are you accessing the file prior to the line of code you provided? If so, how are you accessing it?

您是否在提供的代码行之前访问文件?如果是,您如何访问它?

When accessing files, try to use the following to avoid file stream conflicts:

在访问文件时,尝试使用以下方法来避免文件流冲突:

File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

The last enum FileShare.ReadWrite will allow other file streams to read and write to the file even if you have it open. Of course, it is better to remember to close your stream ASAP.

最后一个枚举文件共享。ReadWrite将允许其他文件流对该文件进行读写,即使您打开了该文件。当然,最好记住尽快关闭流。

http://msdn.microsoft.com/en-us/library/y973b725.aspx

http://msdn.microsoft.com/en-us/library/y973b725.aspx

#2


0  

Do you have the file already open when you get this message?

收到此消息时,文件是否已经打开?

If you do it might be Adobe locking the file.

如果您这样做,可能是Adobe锁定文件。

#3


0  

The likeliest scenario is that something other than ASP.NET / IIS has the file open. Have you ensured that no other processes have a lock on the file when this error occurs?

最有可能的情况是ASP之外的东西。NET / IIS打开了文件。当这个错误发生时,您是否确保其他进程没有对文件进行锁定?

If you have access to the server when the error happens, you can use a tool like Process Explorer to view what exactly is locking the file.

如果发生错误时您可以访问服务器,您可以使用Process Explorer这样的工具来查看锁定文件的确切内容。

#4


0  

The locking can occur from within asp.net itself - as asp.net displays thread-agility, and as such it may be another thread from within the asp.net threadpool completing the request. This would be why you only see this issue intermittently. Baddie's answer is basically the solution to your problem. As an aside, you might find other issues if you're using resources that contain state that are declared as threadstatic. If this is the case you might want to look at making use of CallContext.

这种锁定可以在asp.net内部进行,因为asp.net显示了线程敏捷性,因此它可能是asp.net threadpool中完成请求的另一个线程。这就是为什么您只能断断续续地看到这个问题。巴德迪的答案基本上就是解决你的问题的办法。顺便提一下,如果您使用的资源包含声明为threadstatic的状态,您可能会发现其他问题。如果是这种情况,您可能需要考虑使用CallContext。