Reputation: 14971
I have a text file that contains cached data in JSON format. I'm trying to update the file, but my IIS worker process is locking the file and when I try to write to it, I get the error "The Process Cannot Access The File Because It Is Being Used By Another Process". I've searched for answers on Google and Stack, but I can't seem to find anything related to IIS locking a file. The text file is included in my Visual Studio solution and I am using IIS on my local machine to host my website. For my first question, I'm wondering how I can get IIS to release the file or not lock it at all. For second question, I'm wondering if there's a different approach to prevent IIS from locking my files.
Upvotes: 4
Views: 15677
Reputation: 103
I had a similar problem (file locked by IIS worker process) when attempting to publish a .NET Core API app.
In case it helps anybody, my issue was caused by a related application, which was calling the API, being in debug mode when attempting to publish the API.
Upvotes: 0
Reputation: 1
I was able to fix the problem by having the app pool recycle after each request. Go to the Advanced Settings of your app pool. Under Recycling, set request limit to 1.
Upvotes: -3
Reputation: 14971
After taking a lunch break, I realized that the worker process was hanging on to the text file because it was reading a line from it and then attempted to write to it. I wrapped the file in a using statement and moved the write after the read and now it's working.
Upvotes: 6
Reputation: 161773
IIS is not locking your file. IIS has no idea your file exists. Some application, probably your own, is locking the file.
Try using a program like procmon from SysInternals to find out which process is locking the file.
Upvotes: 7