Web Service Method Attribute Properties

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.
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.
  1. Description
  2. CaheDuration
  3. TransactionOption
  4. BufferResponse
  5. EnbledSession
  6. 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.
  • Description
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.
    •  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;  
       }  
    
       The preceding method will hold the output in a cache for 100 seconds.
      •  TransactionOption
      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:
      1. Disabled
      2. NotSupported
      3. Required
      4. RequiresNew
      5. Supported.
      The following image shows the example of the TransactionOption property.


      The preceding example shows the various options of the TransactionOption Property.
      •  BufferResponse
      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.
        • EnableSession
        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:
        1. InProc
        2. OutProc
        3. Custom
        4. 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.
        •  MessageName
        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.
        •  Note
        If you are beginners to web services then please refer to my following articles:
        1. Introduction to Web Services .
        2. Consuming Web Service in Web Application.
        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.

        Post a Comment

        www.CodeNirvana.in

        Protected by Copyscape
        Copyright © Compilemode