Analysis of method overloading in WebService

Source: Internet
Author: User

This article introduces the Problem Analysis of method overloading in WebService. For more information, see.


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.

[WebMethod]

The Code is as follows: Copy code

Public string GetName ()
{
Return "getting sleepy from an early age ";
}

[WebMethod]
Public string GetName (string strValue)
{
Return strValue;
}

  

2. Why does WebService not support method overloading?

WebService does not support method overloading. 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 corresponding WebService method. After the method is executed, the returned results are encapsulated as XML, that is, the SOAP response, and sent to the client, finally, the client parses the XML section and finally gets the returned results. The key is that the server cannot identify the overloaded method when parsing XML. WebService only recognizes the method name, and the two methods have the same name, 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 Service to identify unique aliases, 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.

 

The Code is as follows: Copy code

[WebServiceBinding (ConformsTo = WsiProfiles. None)]

[WebMethod (MessageName = "FirstMethod")]
Public string GetName ()
{
Return "getting sleepy from an early age ";
}

[WebMethod (MessageName = "SecordMethod")]
Public string GetName (string strValue)
{
Return strValue;
}

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


Method overloading in Webservice


(1) Add a MessageName tag on the WebMethod to be reloaded.
For example:

The Code is as follows: Copy code

[WebMethod (MessageName = "HelloWorld1")]
Public string HelloWorld (){
Return "HelloWorld ";
}

[WebMethod (MessageName = "HelloWorld2")]
Public string HelloWorld (string msg ){
Return msg + "HelloWorld ";
}

(2) modify the WebServiceBinding feature on the class as follows:

The Code is as follows: Copy code
[WebServiceBinding (ConformsTo = WsiProfiles. None)]
Public class UploadService: System. Web. Services. WebService
{
...
}

2. How to pass an object that cannot be serialized as a parameter
For example:

The Code is as follows: Copy code
Void TestMethod (MyObject p ){
...
}

Here, MyObject is a custom class and cannot be serialized. If you have such a method in your WebService, you will prompt "MyObject cannot be serialized, because there is no architecture function without parameters, there are two solutions:
(A) Modify MyObject to serialize it. If MyObject has been encapsulated into an assembly (dll) and cannot be modified, see the second method.
(B) Change void TestMethod (MyObject p)

The Code is as follows: Copy code
Void TestMethod (Object t ){
MyObject p = t as MyObject
...
}

That is, the Object is passed in as a parameter, and then Cast is MyObject in the method. Although this requires additional binning and sealing operations, it is always better than not.

In addition, Let's talk about a few tips. If you want to add a description to the method to make it easier for people who reference webService to understand it, you can go to [WebMethod (MessageName = "HelloWorld1")] then add Desciption = "xxx", that is
[WebMethod (MessageName = "HelloWorld1", Description = "Description, Html syntax supported")]

You can also add descriptions for the entire WebService and add the Desciption attribute to the class, that is

The Code is as follows: Copy code
[WebService (Namespace = "http://www.yourdomain.com/", Description = "service Description, html syntax supported")]
[WebServiceBinding (ConformsTo = WsiProfiles. None)]
Public class UploadService: System. Web. Services. WebService
{
...
}

 

Related Article

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.