Solving Caching problem of IE for WCF REST service

No.of Views3638
Bookmarked0 times
Downloads 
Votes0
By  Dhananjay Kumar   On  16 Feb 2010 00:02:57
Tag : WCF , REST Services
Solving Caching problem of IE for WCF REST service
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Objective

This article will explain; how to solve the caching problem in IE while making call to for REST enabled WCF service. Client might be a SILVERLIGHT client or AJAX client.

Background

Read my other articles on WCF REST service for better understanding of this article.

Is REST service and a web page is same?

Theory says YES, these two are almost the same entity exist on web. Calling a REST service is exactly the same as of browsing the web page on the web.

REST is architecture to create service not a specification. And it works on the HTTP verbs.

REST stands for Representational state transfer where each operation in a service is uniquely identified. Each operation has their, own unique URI and can be accessed via this URL all across the web. Each operation in service definition is attributed with the HTTP verbs they are going to work with. So if URL of a REST service is browsed from the browser, it will return response from the service in form of either JSON or XML base on the service definition.

So, the below link can be browsed in browser.

http://localhost:80/Service1.Svc/GetName?deptId=9

Temporary Internet Files and History Settings,There are four options for cache settings in Internet Explorer 

Image Loading

Every Time I visit the Web Page

On returning of the page which has been viewed previously, IE checks on the server whether viewed page has been changed. If viewed page has been changed, IE will display the new changed page else it will display the cached page.

Note: If this setting is selected in browser there is no need of handle the caching through code. There is no caching for this option.

Every Time I Start Internet Explorer
In this option IE will download the newer version from the server only when browser got reopened or refreshed [F5]. Else each time IE will show the cached page.

Automatically
In this option IE download the new page from the server based on certain algorithm. This is default setting of the browser.

Never
In this option IE never go to server to download the page. Always browser shows the cached page.

Bottom Line is, since browsing web page and REST Service call is exactly the same so browser setting applies to calling REST service also. So caching problem does apply to REST service call also.

How to solve caching problem?
There are two ways to solve the caching problem of REST service call.
1. Through work around
2. Through JQuery

Through Work Around
Let us suppose service is

[WebGet(ResponseFormat = WebMessageFormat.Json)]
[OperationContract]
List GetNames(string deptId);

Service is hosted somewhere and service is being called by, 

AJAX cleint

$.getJSON('Service1.svc/ GetNames',
{ 
deptId: ids },function(data,status){}

Silver Light Client

HttpClient proxyClient = new HttpClient();
proxyClient.TransportSettings.Credentials = CredentialCache.DefaultCredentials;
string targetURI = string.Format("/Service1.svc?deptId={0}",deptId);
HttpResponseMessage response = proxyClient.Get(targetURI);

Now, if service is being called with the deptId=9; then URL of calling service will be http://localhost:80/Service1.Svc/GetName?deptId=9  And a response will get cached at the client browser. 

Problem Statement

Now consider and scenario, when for the deptId =9 database has been changed at the service side. If browser is set as the default (Automatic) and for the next service call with same dept id, browser will call the URL http://localhost:80/Service1.Svc/GetName?deptId=9 , and response for this URL has been cached at the browser, so at the client side changed database won’t be reflected.

Solution

This is a work around solution; just add an extra parameter of type DateTime in service and each time at calling the service pass current date time from the client. So each time there will be a unique URL and that won’t be cached. So each time browser will download the latest response from the server regardless of the browser setting.


Modify service as

[WebGet(ResponseFormat = WebMessageFormat.Json)]
[OperationContract]
List GetNames(string currentDateTime, string deptId);

So now at time of calling the service just pass the current date time and it will modify the Service URL each time. So browser will get the response from the service each time.
http://localhost:80/Service1.Svc/GetName?currentDateTime=12-12-2009:10.00.00,deptId=9


After 1 sec the service call will get modify with the URL and this is new URL .
http://localhost:80/Service1.Svc/GetName?currentDateTime=12-12-2009:10.00.01,deptId=9

Through JQuery

This solution is only applicable for the AJAX client.
1. Call $.ajaxSetup({ cache: false }); before making the service call.
2. Call $.ajaxSetup({ cache: true }); after making the service call. 

$.ajaxSetup({ cache: false });
$.getJSON('Service1.svc/ GetNames',
{ deptId: ids },
function(data,status){}
$.ajaxSetup({ cache: true });

Conclusion

This article explained about working with cache problem in IE while making call to REST enabled WCF service. Thanks for reading.

 
Sign Up to vote for this article
 
About Author
 
Dhananjay Kumar
Occupation-Software Engineer
Company-Infosys Technolgies,Pune
Member Type-Gold
Location-India
Joined date-20 Jul 2009
Home Page-http://dhananjaykumar.net/
Blog Page-http://dhananjaykumar.net/
Dhananjay Kumar is Microsoft MVP on connected system. He blogs at http://dhananjaykumar.net/ . You can follow him http://twitter.com/debugmode_/ and reach him at dhananjay.25july@gmail.com
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top