IIS 6/7 blocks image files while they are being submitted?

I am writing a piece of .NET code that should overwrite image files on a website hosted on IIS 6 or 7. The only processes that should relate to images are IIS and my process that overwrites the image.

I am wondering if IIS will block files at all, causing my rewrite code to throw an exception.

+3
source share
5 answers

The short answer will be an attempt to open the file, if it is not executed, wait a few seconds and try again, looping until success or timeout is reached.

However, if you update these images a lot, you really stun the IIS caching mechanisms. And it's not great for a web server to serve files that are constantly changing. The web server works fine in static files.

Now, if your images are so dynamic, you may need to serve them through a server program. Create your images (or read them from another program through remote access or WCF) and submit it. Your server program may also perform some form of caching. You do not even need to store the image file if it lasts a very short time.

If you only need to replace these images from time to time, then everything is in order to try again.

Now, if these images are really important, and you really want IIS to stop serving the old version, as soon as you have a new version (are you servicing CAPTCHA?), And IIS works with the same file many times per second, then your process may you may not be able to find the slot in. Then you will need to find a way to tell IIS to stop and wait for the new version - restarting it should work, since you do not change images often (otherwise you would go with a dynamic route).

+5
source

Although IIS can lock a file while reading it, you can rename the file and replace it with your new version. This strategy can also be used to troubleshoot the โ€œhalf-written fileโ€ problem when the file begins to be read before it is completely written.

  • Create a replacement file with a temporary file name
  • Rename an existing file from the path
  • Rename the replacement file to its place (for new processes)
  • Delete the old file when the locks are cleared.
+3
source

This is possible, given that although any file is being read, it is locked while reading, so you may encounter a situation where IIS is reading and serving the file, and you are trying to write the file at the same time.

This and this may be of some help in waiting for the file, then lock it. Keep in mind that you can make IIS respond more slowly since it is waiting for a file.

+2
source

IIS will lock files.

How about writing an HttpHandler for images, so you have direct control over how the images are served and where they come from.

Since you control how images are served and how images are replaced, all you need for development is a common locking mechanism to maintain order.

0
source

By serving means, I assume that it reads files and delivers to clients (browser). For this application, you do not need to lock the file at all. Therefore, we can assume that it opens the file in read mode. IIS uses the TransmitFile () API to send files over sockets. It uses the internal OS cache for performance and should not lock the file.

we can see which file is opened in the process using processexplorer from sysinternals. This will help you determine if it is really open by any other process.

0
source

Source: https://habr.com/ru/post/1271930/


All Articles