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.