IIS 6/7是否在服务时锁定图像文件? [英] Does IIS 6/7 lock image files whilst they're being served?

查看:242
本文介绍了IIS 6/7是否在服务时锁定图像文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一段.NET代码,需要覆盖IIS 6或7上托管的网站中的图像文件。应该触摸图像的唯一进程是IIS和我的覆盖图像的进程。



我想知道IIS是否会锁定文件,导致我的覆盖代码抛出异常。

解决方案

简短的回答是尝试打开文件,如果失败,等待几秒钟再试一次,循环直到成功或超时。



<但是,如果你要更新这些图像,那么你真的在这里击败了IIS的缓存机制。 Web服务器提供不断变化的文件并不健康。 Web服务器非常适合提供静态文件。



现在,如果您的图像非常动态,也许您需要通过服务器端程序来提供服务。生成您的图像(或通过远程处理或WCF从其他程序读取)并提供服务。您的服务器端程序也可以执行某种形式的缓存。如果它只持续很短的时间,甚至不需要保留图像文件。



如果您只需要偶尔替换这些图像,那么可以继续重试。



现在,如果这些图像非常重要,并且您真的希望IIS在新版本发布后立即停止提供旧版本(您是否正在为CAPTCHA服务? ),IIS每秒多次提供同一个文件,然后你的进程可能无法找到插槽。然后你要么找到一种方法告诉IIS停止并等待新版本 - - 重新启动它应该可以工作,因为你不经常更换图像(否则,你已经使用了动态路径)。


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

I'm wondering if IIS will lock the files at all causing my overwrite code to throw an exception.

解决方案

The short answer will be to try open the file, if failed, wait for a few seconds and try again, loop until success or timeout.

However, if you are updating these images a lot, you're really defeating IIS's caching mechanisms here. And it is not healthy for the web server to be serving files that are constantly changing. Web servers are great at serving static files.

Now if your images are so dynamic, perhaps you'll need to serve it through a server-side program instead. Generate your images (or read it from another program via remoting or WCF) and serve it. Your server-side program may also do some form of caching. No need even to keep an image file if it only lasts a very short time.

If you only need to replace those images occasionally, then it is ok to keep retrying.

Now if those images are really important, and you really would like IIS to stop serving the old version as soon as you have a new version (are you serving a CAPTCHA?), and IIS is serving this same file many many times per second, then your process may not be able to find a slot in. Then you'll either need to find a way to tell IIS to stop and wait for the new version -- restarting it should work, since you are not changing images often (otherwise, you'd have gone with the dynamic route).

这篇关于IIS 6/7是否在服务时锁定图像文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆