Click here to Skip to main content
15,885,067 members
Articles / Web Development / ASP.NET

ASP.NET Web Services – Different Attribute Properties of a WebMethod

Rate me:
Please Sign up or sign in to vote.
4.25/5 (4 votes)
20 Aug 2014CPOL3 min read 19.4K   2   3
In this post, we will go over different WebMethod attribute properties like Description, BufferResponse and CacheDuration.

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 CalculatorWebServiceAdd and GetCalculations. At the moment, none of these methods display any description.

WebServices1

We want to associate some description with the Add method. To achieve this, specify Description property with the WebMethod.

WebServices2

Let’s view the web service in a browser. Notice that the description we have specified is displayed with the method.

WebServices3

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 BufferResponse to 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 true.

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.

WebServices4

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.

WebServices5

Build the web service and Update Service Reference. This process will regenerate the proxy class.

WebServices6

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.

WebServices7

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.

WebServices8

But if we change the parameters and click on Add method, since these are different parameters, a separate response will be cached.

WebServices9

Reference: Arun Ramachandran (http://BestTEchnologyBlog.Com)

Image 10 Image 11

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
Arun Ramachandran is a Software Engineer having hands on experience in different Microsoft Technologies who is presently working in Experion Technologies, India. He has written over 95 articles on the subject on his blog at http://BestTEchnologyBlog.com. Along with 3 years of hands on experience he holds a Master of Computer Applications degree from Cochin University of Science & Technology (CUSAT).

Comments and Discussions

 
QuestionWhy still asmx? Pin
aSarafian20-Aug-14 20:47
aSarafian20-Aug-14 20:47 
Since 2007 asmx was replaced with wcf, which is more futureproof.

Why are still working on asmx. Explaining the [WebMethod] is a suggesting to people to create legacy applications.
AnswerRe: Why still asmx? Pin
thewazz25-Aug-14 9:22
professionalthewazz25-Aug-14 9:22 
GeneralRe: Why still asmx? Pin
aSarafian25-Aug-14 11:30
aSarafian25-Aug-14 11:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.