Trying to set cache response headers on a dynamically-generated image, I’m getting a weird error:
ArgumentOutOfRangeException was unhandled by user code
Specified argument was out of the range of valid values.
Parameter name: utcDate
I’m reading the file’s last write time from a data cache, and then calling
Response.Cache.SetLastModified(lastWriteTime.Value.ToUniversalTime());
The last write time’s value is {2011-07-25 18:09:56}, as I’d expect… I have no idea why this is going wrong.
The MSDN documentation for this method contains the rather cryptic statement (my emphasis):
The Last-Modified HTTP header time stamps the document with the DateTime value indicating when the document was last modified.
This method will fail if the caching restrictiveness hierarchy is violated.
SetLastModified is introduced in the .NET Framework version 3.5. For more information, see .NET Framework Versions and Dependencies.
What is a caching restrictiveness hierarchy? Am I violating one? If not, why is the last modified time being rejected?
SetLastModified will throw
ArgumentOutOfRangeExceptionif yourlastWriteTimeis in the future.This will succeed:
This will fail:
Decompiling System.Web illustrates this:
Are you generating the
lastWriteTimeon one server and then reading on another where there is a time discrepancy between the servers?Some additional points:
ToUniversalTimeas the framework does that for youUtcSetLastModifiedreduces precision