Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception: Unable to generate etag from dependencies

Tags:

c#

asp.net

iis-7

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);

Sometimes a get an System.Web.HttpException Exception under IIS7 which says:

Unable to generate etag from dependencies. One of the dependencies couldn't generate a unique id.

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?

like image 310
aos Avatar asked May 28 '13 19:05

aos


1 Answers

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);
like image 170
theyetiman Avatar answered Nov 05 '22 11:11

theyetiman