Background
Web Method attribute
Any method in a web service that is exposed to the client application created by marking the WebMethod attribute, because only those methods are exposed to the client marked as a WebMethod attribute. This WebMethod attribute has many properties to enable certain features of the Web Service method. The following are some major properties of WebMethod attributes.
- Description
- CaheDuration
- TransactionOption
- BufferResponse
- EnbledSession
- MessageName
The preceding properties has their own features in webMethod attributes. Let us briefly introduce each of them from the following table.
Attributes |
Description |
Description |
Provides the extra information about Web Method. |
CacheDuration |
This defines the time that how long does web method response will be in cache. |
TransactionOption |
It sets the transaction type whether transactionallowed,NotSupported,Required,Supported, that is supported by namespace System.EnterpriseServices
|
BufferResponse |
It decides whether the method response is buffered ,by default its true. |
EnbleSession |
This determine whether the session is enabled or not .by default its false. |
Let us learn about each off the properties with an example.
This property provides some extra information about the Web Service web method. The following is the basic syntax of defining the description property for the web method attribute.
For example:
- [WebMethod(Description = "This Method Returns Square of given Int Number")]
- public int GetSqureByNumber(int number)
- {
- int ResultedSqure;
- return ResultedSqure = number * number;
- }
In the preceding example we are providing some extra information about that web method, in other words what this method does. When this method is exposed to the client it gives provides the simplicity to identify this method, what it does when you have multiple methods in the web service application. Let us see that in the following image.
From the preceding example when exposing the method to the client it shows some information about that method so it will be easy to identify the method that for what method will be use.
This property specifies how long the web method response will be in the cache when the user makes the request for a specific method. The benefit of the CacheDuration property is it holds the entire output of the method in the cache for a predefined duration.
Suppose the first time the user makes a request to the method for the addition of 20 and 30 it returns from the method and the output is stored in the cache and when the next time a request comes for the same number then the result is returned to the client from the cache instead of executing the function again. The following is the syntax of defining the CacheDuration property for a web method attribute.
For example:
- [WebMethod(CacheDuration = 100)]
- public int GetSqureByNumber(int number)
- {
- int ResultedSqure;
- return ResultedSqure = number * number;
- }
The preceding method will hold the output in a cache for 100 seconds.
This property determines the whether the transaction is used or not in a method that is supported by the namespace System.EnterpriseServices and it must be used.
The following are the some Transaction Options supported by the TransactionOption property:
- Disabled
- NotSupported
- Required
- RequiresNew
- Supported.
The following image shows the example of the TransactionOption property.
The preceding example shows the various options of the TransactionOption Property.
This is a boolean property having true or false values that decides whether or not the output of the request buffer, by default it is true. It is not recommended to buffer the response when the requested output data is huge because the response will become slow when we store a maximum amount of data in the buffer.
The following example shows the syntax and use of the BufferResponse property.
- [WebMethod(BufferResponse=false)]
- public int GetSqureByNumber(int number)
- {
- int ResultedSqure;
- return ResultedSqure = number * number;
- }
The preceding property sets the BufferResponse to false.
This property allows enabling of the session in a XML Web service. Only those web service methods support a session derived from the class System.Web.Services.
It's a boolean property having the values true or false, by default it is false. The following are the session modes supported by the web service method:
- InProc
- OutProc
- Custom
- Off.
The following example shows the syntax and example of the EnableSession property:
- [WebMethod(EnableSession = true)]
- public int GetSqureByNumber(int number)
- {
- int ResultedSqure;
- return ResultedSqure = number * number;
- }
The preceding property enables the session.
This property differentiates the two methods having the same name, the parameters and type for ease of understanding. Let us see the following example having the same name and the same number of parameters that is method overloading.
For example:
- [WebMethod (MessageName="This returns Squre")]
- public int GetSqureByNumber(int number)
- {
- int ResultedSqure;
- return ResultedSqure = number * number;
- }
- [WebMethod (MessageName="This Returns addition of two numbers")]
- public int GetSqureByNumber(int number,int b)
- {
- int ResultedSqure;
- return ResultedSqure = number + number;
- }
In the preceding example I overloaded the
GetSqureByNumber method and at the client site to differentiate the two methods I gave the message of which method does what because if you see in the preceding two methods the names are the same so to avoid difficulty of understanding I have given the message for each method. When you the run code above then see the methods that have the same name.
In the preceding image you have seen the method names are the same but the client doesn't know what method does what. Now click on each method. It shows the message we set for each method name.
Now in the preceding example you see that at the top of the method it's showing the message to provide a better understandability of the method. Now click on the second method, it will show:
See the message at the top, so from all the above examples it is clear that the messageName property is useful for differentiating the methods having the same name.
If you are beginners to web services then please refer to my following articles: