I am trying to get the "if-modified-since" header to work with my WCF web service.
When a user makes a request to my service, I add an ETag to the outgoing response that contains the timestamp of the request as follows:
var tag = String.Format("\"{0:o}\"", new DateTimeOffset(DateTime.Now));
This results in the following ETag header:
ETag: "2011-10-27T13:09:39.6242263-04:00"
I then take this value and echo it back as the if-modified-since header for subsequent requests like this:
If-Modified-Since:2011-10-27T13:09:39.6242263-04:00
When I examine WebOperationContext.Current.Headers.IfModifiedSince, I never get the value provided. The value is fixed at "12/31/1969 7:00:00 PM".
What am I doing wrong?
UPDATE
I should add that using Fiddler, I can set any value to the If-Modified-Since header and still get the same 1969 value in code.
First off,
If-Modified-Since
is about conditional GETs regarding the time of the last modification of the resource, whileETag
is about conditional GETs regarding an identifier of the resources, so please be careful with mixing the two concepts.The correct way of implementing support for If-Modified-Since in a WCF service is to use the
CheckConditionalRetrieve
passing aDateTime
value in theWebOperationContext.Current.IncomingRequest
object - see the code below for that. If the value of the IMS header is before the date you pass toCheckConditionalRetrieve
, the method will exit at that point returning a 304 (Not Modified) response. Otherwise it will just continue. The code below shows that.Another issue: even through the date format you're using (ISO 8601) works, but it's not correct based on the specification (section 14.25 in http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html, and section 3.3.1 in http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3.1), so you should consider using a valid format to prevent future problems.
You can find a good post about conditional GET support in WCF at http://blogs.msdn.com/b/endpoint/archive/2010/02/25/conditional-get-and-etag-support-in-wcf-webhttp-services.aspx.