Introduction:
In this article,i am going to explain about how to caching web methods using webservices and asp.net.
Main:
We can easily caching webmethods using WebmethodAttribute.CacheDuration Property,
for ex,
[WebMethod(CacheDuration = 30)]
When When cache duration is enabled requests and responses are held in memory on the server for at least
the cache duration so caution must be used if you expect requests or responses to be very large or you
expect requests to vary widely.
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
public Service () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string HelloWorld() {
return "Hello World";
}
[WebMethod(CacheDuration = 30)]
public string getdata(string str)
{
return DateTime.Now.ToLongTimeString();
}
}
using System; using System.Web; using System.Web.Services; using System.Web.Services.Protocols; [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class Service : System.Web.Services.WebService { public Service () { //Uncomment the following line if using designed components //InitializeComponent(); } [WebMethod] public string HelloWorld() { return "Hello World"; } [WebMethod(CacheDuration = 30)] public string getdata(string str) { return DateTime.Now.ToLongTimeString(); } } |
Conclusion:
Hope this helps,
Happy Coding.
Nice to see you blogging about this good topic.