After getting lots of response for my webinar on Introduction to Web Services and my article Introduction to Web Services
now I am writing article on Web Service Method attribute
properties especially focusing on students and beginners so let us learn
what they are.
For example:
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.
The preceding method will hold the output in a cache for 100 seconds.
The following are the some Transaction Options supported by the TransactionOption property:
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:
The preceding property enables the session.
For example:
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.
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
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.
- Description
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.
- CacheDuration
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; }
- TransactionOption
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.
- BufferResponse
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.
- EnableSession
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.
[WebMethod(EnableSession = true)] public int GetSqureByNumber(int number) { int ResultedSqure; return ResultedSqure = number * number; }
- MessageName
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 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.
- Note
Summary
From all the examples we have learned about the web method attribute properties. I hope this article is useful for all students and beginners. If you have any suggestion related to this article then please contact me and thanks to all. This is my article number 100. It happened only because of your huge support and responses.
From all the examples we have learned about the web method attribute properties. I hope this article is useful for all students and beginners. If you have any suggestion related to this article then please contact me and thanks to all. This is my article number 100. It happened only because of your huge support and responses.
Post a Comment