Thursday, July 30, 2009

WebService Method Overloading

Thursday, July 30, 2009

WebService Method Overloading

First of all what is method overloading ?
Method means having more than one method with the same name but different signature is the simple definition.
Example:

private int Add(int x, int y)
{
return x + y;
}

private int Add(int x)
{
return x + 3;
}

private float Add(float x, float y)
{
return x + y;
}

Above three methods have the same name but different signature.
The following table shows the signature difference among Add methods.

Though method overloading allowed in .Net, it's not allowed in web services as to comply with a rule (WSI BP 1.1 is that each method within a WSDL document must be unique), This clearly emphasis on method overloading. But by using [WebMethod] attribute we can use method overloading. With [WebMethod] pass the MessageName then web service uniquely identifies the overloaded methods.

Example:

[WebMethod(Description = "Adds two integers.",MessageName = "AddInts")]
private int Add(int x, int y)
{
return x + y;
}

[WebMethod(Description = "Adds 3 on an int value.",MessageName "AddThree")]
private int Add(int x)
{
return x + 3;
}

[WebMethod(Description = "Adds two floats.", MessageName = "AddFloats")]
private float Add(float x, float y)
{
return x + y;
}

Here on above three Add methods have unique MessageName as to allow method overloading in web services

0 comments:

My Achievements

Member of

Blog Archive

Followers