Search content within the blog

Wednesday, April 22, 2009

Enabling caching in Web services using cacheduration and using application object in web services.

Copy paste the code in your respective web services and aspx pages . Because the caching is enabled in the web services for duration of 5 seconds even if multiple refreshes are made within 5 seconds the webservice data which is cahed at the server is presented to the user and fresh data is presented and cached at the end of 5 every seconds.

Webservice code
using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;

namespace SampleService
{
///
/// Summary description for CacheDuration
///

[WebService(Namespace = "http://tempuri.org/",Description="An example to descript cache")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
public class CacheDuration : System.Web.Services.WebService
{

[WebMethod(Description="An example to descript cache",CacheDuration=10)]
public string HelloWorld()
{

if (Application["cachecount"] == null)
Application["cachecount"] = 1;
else
Application["cachecount"] = int.Parse(Application["cachecount"].ToString()) + 1;

return Application["cachecount"].ToString();

}
}
}


ASPX.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using localhost;

public partial class WebServices_EmployeeService : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
CacheDuration objDuration = new CacheDuration();
//objDuration.CookieContainer = objCookieConnt;
Response.Write("Duration:"+objDuration.HelloWorld()+"<br>");
}
}

No comments:

Post a Comment