gpt4 book ai didi

c# - 异常 : Unable to generate etag from dependencies

转载 作者:太空狗 更新时间:2023-10-29 23:00:12 26 4
gpt4 key购买 nike

我在 ASP.NET 中有一个 IHttpHandler,它提供一些图像。处理程序通过以下方式设置 ETAG:

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 异常,它说:

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

但我无法重现该问题(我知道我无法使用 ASP.NET 内部测试 Web 服务器对此进行测试)。有人知道为什么会发生这种情况吗?我可以做些什么来防止这种情况发生?

最佳答案

我无法解释为什么会发生这种情况,但我发现让线程休眠很短的时间可以让文件依赖管理器“把它搞定”并确认在尝试从中创建 eTag 之前,确保新生成的文件确实存在。

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

关于c# - 异常 : Unable to generate etag from dependencies,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16800305/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com