Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / XML

WebMethods - Reference Parameters and OverLoading

Rate me:
Please Sign up or sign in to vote.
3.15/5 (9 votes)
27 Aug 20043 min read 104.8K   17   5
Article about how reference parameters are implemented in web methods and how method overloading is achieved in webmethods

Introduction

Even though we have the concepts of reference parameters and method overloading, when it comes to webservices, there are small differences. In this article, I will go through how reference parameters are implemented in web methods and how method overloading is achieved in webmethods.

Passing Reference Parameters in WebMethods

Passing parameters by reference is a programming concept found in most of today's object-oriented languages. In C#, we can pass reference parameters to a method using the ref modifier. The parameter will not contain a copy of the value, but a reference to the same storage location used by the variable in the calling code. So, a change of value of the reference parameter within the method will be reflected in the original variable after returning from the method. OK, this works fine if you are using a single machine. Mapping this concept to Web services presents a challenge because you're not dealing with memory storage locations anymore. Web service invocations typically span process, machine, or organizational boundaries. When you invoke a Web service operation, you're generating a SOAP message that's transmitted across the wire to another application. The service then returns a response through another SOAP message.

Consider a WebMethod that does not contain any reference parameters:

C#
[WebMethod]
public int AddNum(int x, int y)
{
 return x + y;
}

If you invoke this operation, supplying the value of 3 for x and 5 for y, you'll get the following SOAP response message:

XML
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<AddNumResponse xmlns="http://example.org/sample">
<AddNumResult>8</AddNumResult>
</AddNumResponse>
</soap:Body>
</soap:Envelope>

Let's modify the signature to make x a reference parameter and change the implementation to increment x's value before returning:

C#
[WebMethod]
public int AddNum(ref int x, int y)
{
  return x++ + y;
}

Now, when you invoke this operation using the same values as before, you'll get the following SOAP response message:

XML
<soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<AddNumResponse xmlns="http://example.org/sample">
<AddNumResult>8</AddNumResult>
<x>4</x>
</AddNumResponse>
</soap:Body>
</soap:Envelope>

Notice that x is returned in the response message along with the result. The WSDL file generated by the WebMethod framework contains the XML Schema definition (shown in Figure 1) to describe the elements found in the request and response messages. When you generate a proxy class from this WSDL definition, the .NET Framework is smart enough to figure out that since x is found in both the request and response messages, it can be represented in C# as a reference parameter in the generated proxy class. So, the generated proxy class in this case will contain a method that looks like the following (attributes omitted):

C#
public int Add(ref int x, int y) 
{
object[] results = this.Invoke("AddNum", new object[] {x,y});
x = ((int)(results[1]));
return ((int)(results[0]));
}

Overloading WebMethods

For a language to be called .NET complaint, it should be truly object oriented and therefore must support polymorphism. Method overloading is one of the pillars of polymorphism. Overloading means methods with the same name but different parameters. Just as we overload methods in regular applications, we can overload XML WebMethods also.

Let us create two WebMethods, say HelloWorld. The first one takes no parameters and the second one takes one parameter.

C#
[WebMethod]
public string HelloWorld()
{
 return "HelloWorld";
}

[WebMethod]
public string HelloWorld(string name)
{
 return "HelloWorld " + name;
}

Compile and run it. What do you see in the test page?

Sample screenshot

Because the WebMethods can't have the same name, .NET has a mechanism to differentiate between the methods from one another. We can do this by using MessageName property of the WebMethod. Change the above web method attribute to the following:

C#
[WebMethod (MessageName="HelloWorld")]
public string HelloWorld()
{
 return "HelloWorld";
}

[WebMethod (MessageName="HelloWorldWithName")]
public string HelloWorld(string name)
{
 return "HelloWorld " + name;
}

Compile it and run it. We will see the methods with the name same as the MessageName property.

Sample screenshot

History

  • Version 1.0

License

This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.

A list of licenses authors might use can be found here.


Written By
Software Developer (Senior)
India India
Just another Developer
http://anzboy.wordpress.com/

Comments and Discussions

 
GeneralNot Overloading Pin
SenFo14-Dec-06 8:46
SenFo14-Dec-06 8:46 
Sorry to be picky, but that's not exactly overloading because it renames the method to something else.
GeneralRe: Not Overloading Pin
Lance May6-Aug-07 8:58
Lance May6-Aug-07 8:58 
QuestionOther client platforms ? Pin
Vamsi Prattipati30-Aug-04 5:03
Vamsi Prattipati30-Aug-04 5:03 
AnswerRe: Other client platforms ? Pin
Rahul Deo Bharadwaj13-Apr-08 4:33
Rahul Deo Bharadwaj13-Apr-08 4:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.