Tuesday, December 21, 2004

WebServices Series Part - 2 (WebMethod and WebService Properties)

After creating our basic web service yesterday we will look into the web method properties and supported data types.

Well Whenever we create a web service all those methods of the web service which we want to be web-callable have to be marked with the web method attribute and should have a public access modifier. Web Method Attribute is basically used to make a method web callable.

WebMethod attribute has the following properties like :-


* Description
* TransactionOption
* CacheDuration
* BuffferResponse
* EnableSession
* MessageName




Description :- It is used for commenting the web method. The description that is added here is included in WSDL. Add the

webmethod description property above any method as shown below. When you call the webservice from a browser you view the web service.



e.g. [WebMethod(Description = "My WebMethod Description comes here")]



Enable Session :- WebServices are a type of ASP.NET applications hence you can access your ASP.NET application Session from your webservice. By default the session is disabled i.e. set to false. You can enable it by setting its value to true.



e.g. [WebMethod(EnableSession ="true")]



Message Name :- Message Name property is used give an alias to a method name. This is esp. useful when you are using overloaded methods.



e.g. Yesterday we created 3 method in our webservice for cos,sin and tan which took double as an input parameter. Today we will create their overloads that will take int as an input parameter


[WebMethod]
public double Tan(int angel)
{
return System.Math.Tan(angel);
}

[WebMethod]
public double Sine(int angel)
{
return System.Math.Sin(angel);
}

[WebMethod]
public double CoSine(int angel)
{
return System.Math.Cos(angel);
}



If you compile this code it will compile successfully. It is on execution that you we view the below error message



Error Message: Both Double Tan(Int32) and Double Tan(Double) use the message name 'Tan'. Use the MessageName property of the WebMethod custom attribute to specify unique message names for the methods.



Web service needs to identify each method uniquely hence we will give alias to the methods by setting the MessageName property.



e.g. [WebMethod(MessageName = "SinInt")]
[WebMethod(MessageName = "CosInt")]
[WebMethod(MessageName = "TanInt")]

TransactionOption : - If our web service is a part if any COM/MTS transaction application then we can enable the Transaction Option of the webservice thus making it a part of the 2 phase commit protocol of MTS.



e.g. [WebMethod(TransactionOption = "Supported")]



The dafault is same as COM/MTS transactions i.e. Required.



CacheDuration :- Like any other ASP.NET applications WebServices also support output caching. Output caching gives a performance boost coz the same request is not executed repeatedly instead the result is return from the cache itself. This feature is

esp. useful when you are retrieving chunks of data from the database for listing and other purposes. But caching should preferably used with only those methods where we know that the data will not change frequently.



e.g. [WebMethod(CacheDuration = "180")]

This means the output of this method will be cached for seconds.



BuffferResponse :- We all are familiar with the buffering property rite from our ASP days where used Response.Buffer = true so that

the response is only sent when the Response for the request is completely generated or when the buffer is full. WebServices also use the same logic. If we want our response to be sent across only after its complete processing is done we need to buffer our response by setting its property to true.



e.g. [WebMethod(BufferResponse = "true")]



By default it is set to true.



These properties were webmethod properties. We also have some web service properties like



* Description.
* Name
* NameSpace



Description :- This is same as web method property. It is used to comment web service and give it a description that is included in WSDL.



e.g. [WebService(Description = "My WebService Description comes here")]
public class MyService : System.Web.Services.WebService

{
Name :- By default the name of the class is used as the service name but incase we want to give a different service name which will to exposed to our clients and in WSDL we can set the Name property. This property will override the default value i.e. the class name.



e.g. [WebService(Name = "MyNewName")]
public class MyService : System.Web.Services.WebService
{



NameSpace :- The default name space is http://tempuri.org. You can change this to http://localhost/MyWebServices. You should set namespace to any unique value.



e.g. [WebService(Namespace = "http://localhost/MyWebServices")]
public class MyService : System.Web.Services.WebService
{

No comments: