In the last blog post, we have discussed about how to use session state in a web service. In this article, we will go over different WebMethod attribute properties like
Description, BufferResponse and CacheDuration.
This is a continuation of the previous article. So please go through that before proceeding with this article to get a clear idea. You can read that article here.
Description - Use to specify a description for the web service method.
Let’s understand this property with an example. We have already implemented 2 methods within the
GetCalculations. At the moment, none of these methods display any description.
We want to associate some description with the
Add method. To achieve this, specify
Description property with the
Let’s view the web service in a browser. Notice that the description we have specified is displayed with the method.
BufferResponse – This is a boolean property whose default value is
true. When this property is
true, the response of the XML Web service method is not returned to the client until either the response is completely serialized or the buffer is full.
On the other hand, when this property is
false, the response of the XML Web service method is returned to the client as it is being serialized.
In general, set
false only when the XML Web service method returns large amounts of data. For smaller amounts of data, web service performance is better when
BufferResponse is set to
CacheDuration – Use this property if you want to cache the results of a web service method. This is an integer property and it specifies the number of seconds that the response should be cached. The response is cached for each unique parameter.
Let’s understand the
CacheDuration property with an example. Let’s view the current webform in a browser. At the moment, when we click on Add button, each time the web service method will be processed and response will be returned. Even if we send the same parameters, the web service method will be executed and the response will be sent back.
But when we send the same parameters, there is no point in re-executing it. So it is better to cache the response of the web service method to improve the performance of the application. Use
CacheDuration property for this. If we specify
CacheDuration property to say
20, then the response of the method will be cached for 20 seconds.
Build the web service and Update Service Reference. This process will regenerate the proxy class.
Now view the webform in a browser. For the first time, if we click on
Add method with some parameters, the web service method will be processed and the result should be cached for 20 seconds. If we leave the parameters as the same and click on
Add method again, if we are within the 20 seconds, the web service method will not be processed. Instead, the response which is stored in the cache will be returned to the client.
On the other hand, if the 20 seconds are elapsed, then the web method will be reprocessed and the response will be cached for another 20 seconds.
But if we change the parameters and click on
Add method, since these are different parameters, a separate response will be cached.
Reference: Arun Ramachandran (http://BestTEchnologyBlog.Com)