views:

25

answers:

1

I'm trying to get ASP.Net to cache the response of a web service request by setting the CacheDuration property of the WebMethod attribute:

[WebMethod(CacheDuration = 60)]
[ScriptMethod(UseHttpGet = true)]
public static List<string> GetNames()
{
    return InnerGetNames();
}

The above is a method on an ASP.Net page (I've also tried moving it to its own class, but it didn't seem to make any difference) - I've set UseHttpGet to true because POST requests aren't cached, however despite my best efforts it still doesn't seem to be making any difference (a breakpoint placed at the start of the method is always hit).

This is the code I'm using to call the method:

%.ajax({
    url: "MyPage.aspx/GetNames",
    contentType: "application/json; charset=utf-8",
    success: function() {
        alert("success");
    }

Is there anything that I've missed which might be preventing ASP.Net from caching this method?

Failing that, are there any diagnostic mechanisms I can use to more clearly understand what's going on with the ASP.Net caching?

A: 

Verify that your browser is not sending a Cache-control: no-cache header with its request. According to the documentation, cached results are not sent if the user agent has specified no-cache.

Based on the .ajax call you posted, you should be good, but double checking what is actually sent to the server will let you be sure.

A tool like fiddler is invaluable for debugging exactly what is going on over the wire for browser/web service interaction.

Reed Rector
Checked this already - no cache-control header is sent - I also tried setting the cache-control header myself explicitly, but it made no difference.
Kragen
Try taking a look in perfmon on the server at the ASP.NET Applications counter set, Cache API entries and Output Cache entries (and also perhaps the hits/misses counters in the same area). This will at least tell you if ASP.Net is caching the items but not sending them, or caching the output but for some reason deciding that each page is somehow unique and caching them (e.g. you should see the entries count go up on the first request and the cache hit go up for subsequent if it's working, but if entries increases with each request, it thinks the requests are different for some reason).
Reed Rector