ASP.NET和放大器; IIS7例外:无法生成依赖ETAG [英] ASP.NET & IIS7 Exception: Unable to generate etag from dependencies
问题描述
我在ASP.NET颇有些图像有一个的IHttpHandler
。处理程序设置ETAG方式如下:
I have an IHttpHandler
in ASP.NET which serves some images. The handler sets the ETAG the following way:
context.Response.AddFileDependency(filename);
context.Response.Cache.SetLastModifiedFromFileDependencies();
context.Response.Cache.SetETagFromFileDependencies();
context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.Cache.SetMaxAge(new TimeSpan(999,0,0,0));
context.Response.Cache.SetSlidingExpiration(true);
context.Response.Cache.SetValidUntilExpires(true);
context.Response.Cache.VaryByParams["*"] = true;
byte[] buffer = File.ReadAllBytes(filename);
context.Response.ContentType = MimeMapping.GetMimeMapping(mi.Mi_filename);
context.Response.StatusCode = 200;
context.Response.BinaryWrite(buffer);
有时候得到IIS7下 System.Web.HttpException
除外,它说:
无法生成依赖ETAG。其中一个依赖无法生成一个唯一的ID。
Unable to generate etag from dependencies. One of the dependencies couldn't generate a unique id.
但我无法重现该问题(我知道,我不能和ASP.NET内部测试Web服务器测试此)。有没有人线索,为什么发生这种情况,什么我可以做prevent呢?
But I'm not able to reproduce the problem (I know that I can't test this with ASP.NET internal test web server). Has anybody a clue why this happens and what I can do to prevent this?
非常感谢!
推荐答案
我无法解释为什么出现这种情况,但我发现,使线程睡眠的时间很短的时间内使文件依赖经理的 得到它的奔忙起来并承认新生成的文件,试图从它创建一个ETAG之前确实存在。
I can't explain why this happens, but I found that making the thread sleep for a very short period of time allows the file dependency manager to "get it's sh*t together" and acknowledge that the newly generated file actually exists before trying to create an eTag from it.
...<code to generate "filename" goes here>...
// BUG: For some reason, even though the cache file has definitely been created at this stage, the file dependency manager
// seems to require a bit of time to "register" that the file exists before we add it to the list of dependencies.
// If we don't tell the thread to sleep, we will get an error when it generates the eTag (no idea why this happens - can't find anything on the web).
// If the cache file already existed when this request began, then there is no error.
Thread.Sleep(5);
context.Response.AddFileDependency(filename);
context.Response.Cache.SetLastModifiedFromFileDependencies();
context.Response.Cache.SetETagFromFileDependencies();
context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.Cache.SetMaxAge(new TimeSpan(999,0,0,0));
context.Response.Cache.SetSlidingExpiration(true);
context.Response.Cache.SetValidUntilExpires(true);
context.Response.Cache.VaryByParams["*"] = true;
byte[] buffer = File.ReadAllBytes(filename);
context.Response.ContentType = MimeMapping.GetMimeMapping(mi.Mi_filename);
context.Response.StatusCode = 200;
context.Response.BinaryWrite(buffer);
这篇关于ASP.NET和放大器; IIS7例外:无法生成依赖ETAG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!