How to include caching in WebService ?


Hi
If our requirement is to fetch the same data again and again from database, then we can use caching concept in web service to increasing the performance and avoid the unnecessary hit to database.
Syntax for Caching in web service
[WebMethod(CacheDuration = 5)]
Where 5 is time duration in second.

Complete Syntax is like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

///
/// Summary description for TestCaching
///

[WebService(Namespace = “http://tempuri.org/”)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class TestCaching : System.Web.Services.WebService {

//This syntax is used for cache in webservice
[WebMethod(CacheDuration = 5)]
public string GetServiceTime()
{
return DateTime.Now.ToString();
}

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: