WebMethods Attribute in ASP.NET Web Service


Introduction

WebMethods make the development of XML Web Services easier by encapsulating a good deal of functionality. Attaching the WebMethod attribute to a Public method indicates that you want the method exposed as part of the XML Web service. You can also use the properties of this attribute to further configure the behavior of the XML Web service method. The WebMethod attribute is added to each method we want to expose as a Web Service. ASP.NET makes it possible to map traditional methods to Web Service operations through the System.Web.Services.WebMethod attribute and it supports a number of properties that control the behavior of the methods.

The following properties are available.

  • CacheDuration
  • Description
  • EnableSession
  • MessageName
  • TransactionOption
  • BufferResponse

In this article I am going to describe CacheDuration only.

CacheDuration

ASP.NET has built-in support for caching the data on the server. Web Services can use the caching
support of ASP.NET to cache the result of a web method.

CacheDuration is the number of seconds the response should remain in the cache. The default is 0, which means the server doesn't cache the response. When you enable caching, the server holds responses in memory for the cache duration, so you must use caution if you expect responses to be large or if you expect requests to vary widely. To set the CacheDuration to 20 seconds, you use code such as.

[WebMethod(CacheDuration = 20)]

Creating a Simple Web Service

Use the following steps to create a web service.

  • Go to Visual Studio 2010 and create a New Project.

img1.gif
  • Select .NET Framework 3.5.
  • Create an ASP.NET Web Service Application.
  • Give it a name and click ok button.

img2.gif

Add the following method in the Service.cs file.

[WebMethod(CacheDuration = 10)]
    public string GetTime()
    {
        return DateTime.Now.ToString("T");
    }

The complete Service.cs file is look like as.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
 [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 Service : System.Web.Services.WebService
{
    public Service ()
    {
        //Uncomment the following line if using designed components
        //InitializeComponent();
    }
    [WebMethod(CacheDuration = 20)]
    public string GetTime()
    {
        return DateTime.Now.ToString("T");
    }
}

The duration is specified in seconds. The example above sets the cache to expire after 20 seconds. The
default value is 0, which means that caching is disabled.

Build Web Service and Run the Web Service for testing by pressing F5 function key.

img3.gif

Click on GetTime.

img4.gif

To test the operation using the HTTP POST protocol, click the 'Invoke' Button.

img5.gif

If the web method takes parameters, the parameters are used as keys for the cache. The result will be
cached for each combination of the parameters. Here is an extension of the GetTime() function above
that takes the format string as a parameter.

 [WebMethod(CacheDuration=20)]
   
public string GetTime(string format)
      {
          return DateTime.Now.ToString(format);
      }

The result will be different for each format string. If the format string is 'd' the time will be returned in
the format 'M/d/yyyy', if the format string is 'g', the time will be returned in the format 'M/d/yyyy
HH:mm aa' and so on. If the web method is called within 10 seconds with the same format string, the
result will be fetched from the cache.

Again Build Web Service and Run the Web Service for testing by pressing F5 function key Click on GetTime.

img6.gif

'd' into the TextBox.

img7.gif

click the 'Invoke' Button.

img8.gif

Again Rebuild the web service follow above step and 'g' into the TextBox.

img9.gif

click the 'Invoke' Button.

img10.gif

Again Rebuild the web service and 'm' into the TextBox.

img11.gif

click the 'Invoke' Button.

img12.gif

Same as you can 'y' and get.

img13.gif
Resources