views:

84

answers:

4

In my ASP.NET app, I noticed that the results of any web service calls are cached. I don't want any results to be cached, how can I stop the browser from caching the results?

Update:

Here's the proxy code generated by calling the web service URL appending '/js', e.g. /mywebservice.asmx/js

var MyWebService=function() {
MyWebService.initializeBase(this);
this._timeout = 0;
this._userContext = null;
this._succeeded = null;
this._failed = null;
}
MyWebService.prototype={
SomeWebMethod:function(itemID,succeededCallback, failedCallback, userContext) {
return this._invoke(MyWebService.get_path(), 'SomeWebMethod',false,
{itemID:itemID},succeededCallback,failedCallback,userContext); }}
MyWebService.registerClass('MyWebService',Sys.Net.WebServiceProxy);
MyWebService._staticInstance = new MyWebService();
MyWebService.set_path = function(value) { MyWebService._staticInstance._path = value; }
MyWebService.get_path = function() { return MyWebService._staticInstance._path; }
MyWebService.set_timeout = function(value) { MyWebService._staticInstance._timeout = value; }
MyWebService.get_timeout = function() { return MyWebService._staticInstance._timeout; }
MyWebService.set_defaultUserContext = function(value) { MyWebService._staticInstance._userContext = value; }
MyWebService.get_defaultUserContext = function() { return MyWebService._staticInstance._userContext; }
MyWebService.set_defaultSucceededCallback = function(value) { MyWebService._staticInstance._succeeded = value; }
MyWebService.get_defaultSucceededCallback = function() { return MyWebService._staticInstance._succeeded; }
MyWebService.set_defaultFailedCallback = function(value) { MyWebService._staticInstance._failed = value; }
MyWebService.get_defaultFailedCallback = function() { return MyWebService._staticInstance._failed; }
MyWebService.set_path("/MyWebService.asmx");
MyWebService.SomeWebMethod= function(itemID,onSuccess,onFailed,userContext)
{MyWebService._staticInstance.SomeWebMethod(itemID,onSuccess,onFailed,userContext); }

I call the service using:

MyWebService.SomeWebMethod(
itemID,
function(sender, e)
 {
    // do something
},
function(sender, e)
{
   // handle failure
});

I use the suggested technique (append a param to the URL with some random value) with others pages to prevent them from being cached, but I'm surprised that I need to use this technique with a web service call considering that they are POST calls by default in ASP.NET 2.0 and shouldn't be cached in the first place.

A: 

When you make a request to a web service, append a unique value to the querystring, such as the current date and time converted to a numeric value, this will ensure that it's treated as a unique request each time and no caching of responses will take place.

Specifically the Javscript Date.getTime() method:

var d = new Date();
var uniqueValue = d.getTicks();

// Now append uniqueValue to the querystring you're calling the webservice with
// and it'll ensure the request is treated as unique by the browsers cache
Rob
But how can I do this? I call the service on the client using javascript.
JJJ
On the client, I call the service using the proxy code generated when you call the service appending '/js' to the end of its URL. Should I change the proxy code to make it add the time/date in javascript?
JJJ
Add an example of the code you're using to call the service into your question if you can't figure out how to add this to it, I'll take that and work it into my answer =)
Rob
I've never used the ap.net web service proxies, so I'd guess (but can't be certain!) that that's as good a bet as any =)
Rob
Thanks a lot. Just another question, how are the results cached in the first place? Don't ASP.NET use POST calls that shouldn't be cached in the first place (AFAIK, they made a change in ASP.NET 2.0 to use POST by default)? Also, I see there's another option to prevent caching by changing the [WebMethod] attribute (to send no-cache in the cache control header). What are the advantages/disadvantages of this compared to changing the URL of the web service on the client?
JJJ
Finally, is there another attribute for the whole service to do the same? I mean instead of changing the [WebMethod] attribute for every method in the web service? Thanks and sorry for my too many questions.
JJJ
As I said, I've never used web service proxies, so I'm afraid I don't know if there's an Attribute based method for either specific methods, or the whole service. Sorry! =) I'm not sure why it's being cached, for the same reason, and would suggest that you could always use something like Fiddler (http://www.fiddler2.com/fiddler2/) to help determin why, i.e. to verify if it's making a POST request or not.
Rob
Thanks a lot. I'll have a look on it with Fiddler.
JJJ
+1  A: 

how can I stop the browser from caching the results?

Could it be that you're using ASP.NET ajax? If so then I think you mean that the ajax calls get cached by the browser. If so then take a look at this: http://yoavniran.wordpress.com/2010/04/27/ie-caching-ajax-results-how-to-fix/.

XIII
Thanks, I'll have a look on that. But actually the results are cached in all browsers not just IE.
JJJ
I know but the main topic is that you're using ajax and that you can turn of caching. Take a look at the article.
XIII
+2  A: 

If you have control over the server, look here for IIS7

On older versions of IIS add a

Cache-Control: no-cache 

to the headers for the ASMX / SVC file (or for the whole directory)

nonnb
A: 

I guess you are using IE... is a pita regarding cache control.

The cache-control header can be managed on a request by request basis by using the response object. Be aware that you can't modify the response header once you have issued the first byte of response body.

VdesmedT