Method Overloading in Web Services

The method overloading will not simply work in Web services. If you will try to add two web method in web service with the same method name as below

[WebMethod()]
public int Add(int a, int b)
{
  return (a + b);
}
[WebMethod()]
public float Add(float a, float b)
{
  return (a + b);
}

Will not work. And will show you below error.

error image

To fix it, you have to do a tweak.

You have to use Web method MessageName property as below

[WebMethod(MessageName = “<name>”, Description = “<description>”)]

So it becomes

[WebMethod(MessageName = “Add_Integers”, Description = “Overloads for adding integers”)]

And

[WebMethod(MessageName = “Add_Floats”, Description = “Overloads for adding floats”)]

Actually the issue is with the WSDL not with the web service. Web service allows us to overload method but WSDL is not able to generated a valid document with the same method names and get confuses.

Finally i do something like below

[WebMethod(MessageName = “Add_Integers”, Description = “Overloads for adding integers”, EnableSession = true)]
    public int Add(int a, int b)
    {
        return (a + b);
    }
    [WebMethod(MessageName = “Add_Floats”, Description = “Overloads for adding floats”, EnableSession = true)]
    public float Add(float a, float b)
    {
        return (a + b);
    }

That’s it. enjoy method overloading in web services or reuse your existing business layer.

Tarun Juneja

Advertisements

2 thoughts on “Method Overloading in Web Services

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s