Method overloading in WebService

Source: Internet
Author: User

Reading directory

I. Can methods in WebService be reloaded?

2. Why does WebService not support method overloading?

3. How to Solve WebService method overloading?

 I. Can methods in WebService be reloaded?

    WebService does not support method overloading. We can draw a conclusion from the image.

1 [WebMethod] 2 public string GetName () 3 {4 return "sleepy since childhood"; 5} 6 7 [WebMethod] 8 public string GetName (string strValue) 9 {10 return strValue; 11}

    

2. Why does WebService not support method overloading?

    WebService does not support method re-loading. This must be mentioned in the working mechanism of WebService. When the client calls a WebService method, first, you need to package the method name and parameters to be passed into XML, that is, the SOAP package, which is passed to the server through the HTTP protocol, and then the server parses the XML section, obtain the called method name and passed parameters, and then call the method corresponding to WebService. After the method is executed, the returned results are encapsulated as XML, that is, the SOAP response, sent to the client, and the client parses the XML section to get the returned result. The key is that the server cannot identify the overloaded method when parsing XML. WebService only recognizes the method name, the two methods have the same name, and the server does not know which method to call.

3. How to Solve WebService method overloading?

The MessageName attribute can be used to eliminate the issue that the Web service cannot recognize due to multiple identical names. Because the MessageName attribute enables the Web server to determine the unique alias for the overload method, by default, the method name is used. When the MessageName attribute is specified, SOAP will reflect the value of MessageName, rather than the method name itself. Therefore, this solves the problem of overloading of methods not supported in WebService.

1 [WebServiceBinding (ConformsTo = WsiProfiles. none)] 2 [WebMethod (MessageName = "FirstMethod")] 3 public string GetName () 4 {5 return "sleepy since childhood "; 6} 7 8 [WebMethod (MessageName = "SecordMethod")] 9 public string GetName (string strValue) 10 {11 return strValue; 12}

Now we can see that the message names of these two methods are not separated.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.