0

I have a long running web service (3-5 mins) which returns a large custom object.

I declare the web method like this...

public class MyService : System.Web.Services.WebService
{
    [WebMethod(CacheDuration=86400)]
    [XmlInclude(typeof(MyObject))]
    public MyObject Items(string myParam)
    {
        return new MyObject(myParam);            
    }
}

I then consume the service in a classic ASP.NET 4.0 app and cache the elements of "Items" like this...

protected MyService.MyObject servObj = new MyService.MyObject();
protected void Page_Load(object sender, EventArgs e) 
{  
    if (!Page.IsPostBack) 
    {
        if (Cache["elems1"] == null) 
        {
            MyObject items = servObj.Items("myParam");
            Cache.Insert("elems1", servObj.elems1, null, DateTime.MaxValue, TimeSpan.FromHours(24));
            Cache.Insert("elems2", servObj.elems2, null, DateTime.MaxValue, TimeSpan.FromHours(24));
        }
    }
}

When I run the app it seems that the web service cache is sometimes available and sometimes not i.e. it is not lasting the specified duration. The web service runs in it's own app pool on IIS.

Any ideas why? Is there a smarter way to cache the web service results?

cymorg
  • 534
  • 2
  • 10
  • 27
  • You're only populating the cache when not a PostBack. Are you sure you're not trying to reference items in the cache prior to them actually being placed in the cache? What if your Web Service throws an error? Nothing is going to be placed into the cache. – Laurence Adams Mar 24 '15 at 16:24
  • IIS by default shuts down an application after 20 minutes of inactivity. Which if you haven't changed that value it is far shorter than the duration you've specified there. – RandomWebGuy Mar 24 '15 at 16:24
  • I've handled web service failure errors elsewhere. IIS is configured to timeout the application at 20hrs yet it seems to timeout after about 20 mins. Weird. – cymorg Mar 24 '15 at 16:48
  • @cymorg 20 hours of inactivity or recycle every 20 hours? There are two different settings: http://stackoverflow.com/a/19985866/612036 . Just double checking to make sure as that's the first thing I'd check. – RandomWebGuy Mar 24 '15 at 16:54

1 Answers1

0

Turns out that Cache.Insert(...) is in the Page class. It needs to be Context.Cache.Insert to be in the Application cache. Doh!

cymorg
  • 534
  • 2
  • 10
  • 27