Click here to Skip to main content
Click here to Skip to main content

WebMethods - Reference parameters and OverLoading

, 27 Aug 2004
Rate this:
Please Sign up or sign in to vote.
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:

[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:

<soap:Envelope
xmlns:soap="<A href="http://schemas.xmlsoap.org/soap/envelope/">http://schemas.xmlsoap.org/soap/envelope/</A>">

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

[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:

<soap:Envelope
xmlns:soap="<A href="http://schemas.xmlsoap.org/soap/envelope/">http://schemas.xmlsoap.org/soap/envelope/</A>">

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):

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 said .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.

[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:

[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

Share

About the Author

Ansil
Software Developer (Senior)
India India
Ansil hails from Trivandrum.
He works for Microsoft india (R&D ).
he blogs at http://ansilf.spaces.live.com/
 

 
"First they ignore you;then they laugh at you ;then they fight you ;then you win "

Comments and Discussions

 
GeneralNot Overloading PinmemberSenFo14-Dec-06 9:46 
GeneralRe: Not Overloading PinmemberLance May6-Aug-07 9:58 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141223.1 | Last Updated 28 Aug 2004
Article Copyright 2004 by Ansil
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid