Caching web service results
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 ie 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?
Turns out that Cache.Insert(...)
is in the Page class. It needs to be Context.Cache.Insert
to be in the Application cache. Doh!
上一篇: 使用不兼容的Visual Studio调试,但使用.pdb
下一篇: 缓存Web服务结果