.NET Framework and Web Services - Part 3

In .NET Framework and Web Services - Part 2 we learned how to create a Web service and how to consume it from a VB.NET client. Here I am going to explain Web methods. A WebMethod represents a Web method. WebMethod has 6 properties they are 

  1. Description
  2. MessageName
  3. EnableSession
  4. CacheDuration
  5. TransactionOption
  6. BufferResponse

WebMethod Example in VB.NET

<WebMethod()> Public Function SayHello()
Return "Hello Ragavan"
End
Function

<WebMethod> This Attribute tells that this SayHello function exposable over the net. This type of method called WebMethod. 

Example in C# 

[WebMethod]
public string SayHello ()
{
return "Hello Ragavan";
}

Description 

Both the [WebService], and [WebMethod] attributes have a Description Property. With this property we can associate documentation with our web Service and WebMethod. 

For example you can use Description Attribute to describe the Webmethod.  

[WebMethod (Description="This method will add three integer")]
public int Add (int a, int b, int c)
{
return a+b+c;
}

MessageName

This property useful when we want overloading  the WebMethods. 
For Example 

<WebMethod()> Public Function SayHello(ByVal sName As String) As
String
Return "Hi" & " " & sName
End
Function

<WebMethod(MessageName:="SayHello")> Public Function SayHello() As
String
Return "Hi Ragavan"
End
Function

EnableSession

This Property used for to enable the session in WebServices.
(Remember WebServiecs uses HTTP protocol this is stateless) .if we want to maintain the state between client and server we need to use this property.  Default Enablesession is false.

[WebMethod (EnableSession=true)]
public string SayHiToMS ()
{
return " Hello to .NET ";
}

CacheDuration

When we cache the previously accessed result. Next time the same user or different user   asks we can serve from cache. Here result cached 60 milliseconds. if we invoke this method with in 60 milliseconds it will print same time . This will increase the Web Service performance. 

<WebMethod(CacheDuration:=60)> Public Function PrintTime() As
String
Return Now().ToString
End
Function

TransactionOption

TransactionOption Can be one of five modes:

  • Disabled
  • NotSupported
  • Supported
  • Required
  • RequiresNew

Even though there are five modes, web methods can only participate as the root object in a transaction. This means both Required and RequiresNew result in a new transaction being created for the web method. The Disabled, NotSupported, and Supported settings result in no transaction being used for the web method. The TransactionOption property of a web method is set to Disabled by default. 

BufferResponse

(boolean) Controls whether or not to buffer the method's response 

WebServices Capability

Capability Web services
Invoke single method on a stateless object Yes
Invoke multiple methods on a stateful object No 
Have all clients invoke methods on the same serverside object No 
Pass through firewalls Yes
Uses HTTP for communication Yes
Uses raw TCP socket for communication No
Use IIS as host Yes
Allow custom host No
Uses SOAP-compliant formatting of data Yes
Uses smaller binary formatting of data No
Retrieve partial copy of data from complex object Yes
Retrieve complete copy of complex object No


Similar Articles