Webservices – Method Overloading

Now webservice means software system designed to support interoperable machine-to-machine interaction over a network, also Overloading means method name remains same but the signatures must be different

Lets start doing it bit coding way ! Create web service WebServiceOverLoad by default webservice create
HelloWorld() method default,we start with overloading this method.as given below.

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WebServiceOverLoad : System.Web.Services.WebService {

public WebServiceOverLoad () {

//Uncomment the following line if using designed components
//InitializeComponent();
}

[WebMethod]
public string HelloWorld() {
return “Hello World”;
}

[WebMethod]
public string HelloWorld(string name)
{
return “Hello” + name;
}

[WebMethod]
public string HelloWorld(string name,int one)
{
return “Hello” + name + ” ” + one;
}
}

when we compile this webservice we will get the following server error , suggestiong that we need to enter MessageName property of the WebMethod() for the overloaded methods.

Both System.String HelloWorld(System.String) and System.String HelloWorld() use the message name ‘HelloWorld’. Use the MessageName property of the WebMethod custom attribute to specify unique message names for the methods.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: Both System.String HelloWorld(System.String) and System.String HelloWorld() use the message name ‘HelloWorld’. Use the MessageName property of the WebMethod custom attribute to specify unique message names for the methods.

So after the activation of the MessageName property as follows

[WebMethod (MessageName="With name")]
public string HelloWorld(string name)
{
return “Hello” + name;
}

[WebMethod (MessageName="With name and number")]
public string HelloWorld(string name,int one)
{
return “Hello” + name + ” ” + one;
}

so after compilation

Service ‘WebServiceOverLoad’ does not conform to WS-I Basic Profile v1.1. Please examine each of the normative statement violations below. To turn off conformance check set the ConformanceClaims property on corresponding WebServiceBinding attribute to WsiClaims.None.

Now we got this error because as we are using WebServiceBinding as default , it is binding to

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] make it to WsiProfiles.None

[WebServiceBinding((ConformsTo = WsiProfiles.None)]

So this way we can do Method Overloading in WebServices.

In short MessageName & WsiProfiles.None is the main to remeber.

About these ads

5 responses to “Webservices – Method Overloading

  1. thanx a lot…saved a lot of time…
    good job !!!

  2. You have one extra ( in WebServiceBinding. Thanks.

    [WebServiceBinding((ConformsTo = WsiProfiles.None)] should be
    [WebServiceBinding(ConformsTo = WsiProfiles.None)]

  3. This was very helpful and concise!

  4. Otimo post.
    Pesquisei muito e só consegui resolver quando cheguei aqui.
    Muito obrigada…

  5. Amazing work.

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