Solving Caching problem of IE for WCF REST Service

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/Service1.Svc/GetName?deptId=9

Temporary Internet Files and History Settings

There are four options for cache settings in Internet Explorer

image1.gif

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<string> GetNames(string deptId);


Service is hosted somewhere and service is being called by,

AJAX client

$.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/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<string> 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.

Up Next
    Ebook Download
    View all
    Learn
    View all