I am using an instance of WebClient in my Silverlight application to retrieve an image file for display. In order to service this application, I have set up a WCF REST service. The following is a snippet of code I wrote for that service:
DateTime modSince = WebOperationContext.Current.IncomingRequest.Headers[HttpRequestHeader.IfModifiedSince]
== null
? DateTime.MinValue
: DateTime.Parse(WebOperationContext.Current.IncomingRequest.Headers[HttpRequestHeader.IfModifiedSince].ToString());
FileInfo fInfo = new FileInfo(filePath);
if (fInfo.LastWriteTime > modSince)
{
WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg";
WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.OK;
WebOperationContext.Current.OutgoingResponse.LastModified = fInfo.LastWriteTime;
}
else
{
WebOperationContext.Current.OutgoingResponse.SuppressEntityBody = true;
WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.NotModified;
}
The service is set up such that on a request for an image file, the if-modified-since attribute of the request header is examined. If this value is less than the last time the file is modified, the file is returned. Otherwise, a 304 message is returned.
We are interested in making the transaction faster. Since the images rarely, if ever, change it is desirable to look up the cache for a copy of the image before making a request to the server, which would save on network latency. In trying to implement this for IE, I have changed the Temporary Internet Files settings to "never check for newer versions of stored pages". When I enter in the browser's address bar the REST service URL for an image that has a copy stored in the cache, the copy is retrieved from the cache without the server being called. However, when the URL is entered as a parameter to the WebClient's OpenReadAsync method a GET request is sent to the server, leading to a 304 message.
Is there any way I could use the WebClient to look up the cache without a GET request being sent to the server?