title: Caching web service results
body: <p>I have a long running web service (3-5 mins) which returns a large custom object.</p>
  
  <p>I declare the web method like this...</p>
  
  <pre><code>public class MyService : System.Web.Services.WebService
  {
      [WebMethod(CacheDuration=86400)]
      [XmlInclude(typeof(MyObject))]
      public MyObject Items(string myParam)
      {
          return new MyObject(myParam);            
      }
  }
  </code></pre>
  
  <p>I then consume the service in a classic ASP.NET 4.0 app and cache the elements of "Items" like this...</p>
  
  <pre><code>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));
          }
      }
  }
  </code></pre>
  
  <p>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.  </p>
  
  <p>Any ideas why?  Is there a smarter way to cache the web service results?</p>