Wednesday, April 6, 2011

How to force HttpWebRequest to use cache in ASP.NET environment?

In my ASP.NET app I use HttpWebRequest for fetching external resources which I'd like to be cached. Consider the following code:

var req = WebRequest.Create("http://google.com/");
req.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.CacheIfAvailable);
var resp = req.GetResponse();
Console.WriteLine(resp.IsFromCache);
var answ = (new StreamReader(resp.GetResponseStream())).ReadToEnd();
Console.WriteLine(answ.Length);

HttpWebRequest uses IE cache, so when I run it as normal user (in tiny cmd test app), data is cached to %userprofile%\Local Settings\Temporary Internet Files and next responses are read from cache.

I thought that when such code is run inside ASP.NET app, data will be cached to ...\ASPNET\Local Settings\Temporary Internet Files but it is not and cache is never used.

What I am doing wrong? How to force HttpWebRequest to use cache in ASP.NET environment?

From stackoverflow
  • I could be wrong but I would suspect that HttpWebRequest respects cache headers from the resource regardless of your stated desires.

    Check the response headers and ensure that the resource is not explicitly asking to not be cached.

    piotrsz : I suspected headers too so I build a tiny cmd app that makes httprequest to the same resource. In cmd app it's cached. And in ASP.NET app it's not.
  • You can use the cache manually in your code like this:

     Cache.Insert("request", req, Nothing, DateTime.Now, TimeSpan.FromSeconds(30), TimeSpan.Zero)
    

    You can use this Method like you would use caching with in web.config.

    piotrsz : @MUG4N: I'll probably end up implementing custom cache but I was hoping for some straightforward "out of the box" solution...
    MUG4N : I think there is no out of the box for your problem but this shouldn't be a big problem to implement.
  • As I put in the comment above, add a line to dispose the response object...

    resp.Close();
    

    and then see if you still have problems.

    If you do, creating a log with the following app.config in your vdir might help figure out why it is not being cached...

    Creating a system.net debugging tracelog

    Note, that the IE cache is really not meant to be used in middle-tier scenarios. You are doing something that is not supported by microsoft.

    piotrsz : I put response into `using` block so it gets closed and disposed and data is cached now! Thanks :-) But I'll probably use different approach as you and MUG4N suggeseted.
    feroze : Glad it worked for you. I would look into IsolatedStorage in .NETFramework to see if it provides the functionality on top of which you could build your cache.

0 comments:

Post a Comment