Properties Of WebMethod Attribute In Web Service - Part Four

WebMethod

This attribute is used to customize the behavior of your Web Service. This attribute holds up several properties like enable session, description, cacheduration, and bufferresponse. I discussed the enable session property in my previous article. Before proceeding with this article, you should follow my ASP.NET Web Service series from the following links,

  
 

CacheDuration
 
We use this property, only when we want the result of a Web Service method in durations. CacheDuration is an integer property. Let’s understand it with an example -

Every time we click on the output button, the Web Service method will be processed and response will be returned from that Web Service method. Hence, even if we send same parameter, still the Web Service method will be executed and the response will be sent back.
 


When we send the same parameters to the Web Service method, there is no point in re-executing it, because we know that we got the same response, so it is better to cache the response of the Web Service method. Hence, if you want to cache the response of the Web Service method, all we need to do is:
 


If you specify cache duration as 15 then the response of this method will be cached for 15 seconds. Therefore, build your Web Service , which is MyWebService.asmx and update your Service reference. This process would regenerate the proxy class and completely update it.
 


Now, click output button,and the first time when we send the parameters (Amit and Mishra), the Web Service method will be reprocessed and then the result of Web Service method should be cached for 15 seconds. Now, if we leave same parameters (Amit and Mishra) and click output button once again and if we are within 15 seconds, then the Web Service will not be reprocessed. Instead, the response is stored in the cache and will be returned to the client. On the other hand, if the 15 seconds have been spent then the Web method will be reprocessed and then that response will be cached for another 15 seconds.

If you’re using different parameters, after making a request within 15 seconds, the changes will happen.
 


Description

This property specifies some additional information about the Web method, and if you want to describe your Web method or you want to tell anyone more about your Web method, you can use this property.

If you recollect from previous articles, we implemented three methods within the MyWebService that are AddName, GetResult and Sub, so none of these methods display any description. Consider the image, shown below:
 


Now, I want to associate some descriptions for these methods. For this, I need to specify the description in each method.



After specifying the description, right click Service and choose view in the Browser.
 


Notice the description that we have specified, which is displayed on the Service page.
 


BufferResponse

This property is a Boolean property, as it takes bool value. Its default value is true. Asthe name suggests, when XML Web Service stores maximum amount of data in the buffer, its performance slows so set it to false. For smaller amount of information, set it to true.



Message Name
 
This property specifies the difference between the two same methods or you can say, this property is just like the description property. The only difference is that the message name is also displayed inside the method name whereas in the description property, it does not. It is used for overloading to a Web Method.
 


In the following screenshot, the changes can be seen like using Message name property in only AddName method and other methods. Hence, click AddName method.
 


This following screenshot shows the message as well that we set in AddName method. The benefit of this property is that it provides a better understanding of our method.
 


EnableSession
 
This property specifies whether the session is enabled or not. If you want to use session property in your Web Service, then you can set it to true. Hence, the session is derived from the class System.Web.Services. This is a bool property.
 


Using this property, we can use session in a Web Service (Web Method).

Thanks for reading this article, stay tuned with us for more on Web Services.


Similar Articles