top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Overloading Web Methods In A Web Service

0 votes
303 views

Introduction

Web services are also classes just like any other .NET classes. However, they typically have methods that are marked as web methods. In addition to web methods they can also contain normal methods.

Since a web service is a class it can utilize all the OO features like method overloading. In this article and demo we will see how to use method overloading in a web service.

Creating web methods

We will create a simple web service that has following three overloaded methods :

  • Public Function GetWelcomeMessage() As String
  • Public Function GetWelcomeMessage(ByVal name As String) As String
  • Public Function GetWelcomeMessage(ByVal name As String, ByVal dob As DateTime) As String

All the three methods return variants of a welcome message to the web service client. Next, we will mark two methods as web methods but keep last method as normal method of the class. To mark the methods as web methods just use following syntax :

VB.NET
<WebMethod()>
Public Function GetWelcomeMessage() As String
C#
[WebMethod()]
public string GetWelcomeMessage()
{...}

Compile the class and run it in the browser (I assume that you are using VS.NET). You should get a error saying that GetWelcomeMessage method must be marked with MessageName attribute. This is because even though our class can distinction between overloaded methods when we call them over SOAP there should be some unique identification for each one.

Adding MessageName attribute

To overcome the problem we will add MessageName attribute to the second web method as shown below :

VB.NET
<WebMethod(MessageName:="GetWelcomeMessageWithName")>
Public Function GetWelcomeMessage(ByVal name As String) As String
C#
[WebMethod(MessageName="GetWelcomeMessageWithName")]
Public Function GetWelcomeMessage(ByVal name As String) As String

Now, compile the service again and run in the browser. You will notice that you get GetWelcomeMessageWithName listed rather than GetWelcomeMessage for the second web method. In all your client code you will use this method name instead of original method name. Your typical client code will look like this :

'I assume that you have created web service proxy
'either by WSDL utility or adding a web reference
'in VS.NET
Dim x as new MyWebServices.Service1()
'call first web method
dim str1 as string=x.GetWelcomeMessage()
'now call overloaded version
dim str2 as string=x.GetWelcomeMessageWithName()

Note that in VB.NET MessageName attribute is represented by 'named argument'.

The third method is not marked as a web method and hence can be used just like any overloaded method inside the web service class.

posted Dec 31, 2016 by Shivaranjini

  Promote This Article
Facebook Share Button Twitter Share Button LinkedIn Share Button


Related Articles

Introduction

While developing clients for web services, we typically add a web reference to the web service by specifying URL of the .asmx file. Adding a web service in VS.NET generates required proxy object. However, it may happen that after adding the web reference the web service is moved to some other location. In such cases the most easy way is to recreate the proxy object. But what if the same thing happens after you deploy your web service client. It would be nice to allow configurable URL so that even if original web service is moved your clients need not be recompiled. In this article we will see how to do just that.

Creating the web service

For our example we will develop a simple web service that has only one method. Following steps will show you how to proceed.

  • Create a new C# web service project in VS.NET.
  • Open the default .asmx file and add following code to it.
using System;
using System.Web.Services;

namespace HelloWorldWS
{
public class CHelloWorld : 
System.Web.Services.WebService
{
	[WebMethod]
	public string GetHelloWorld()
	{
		return "Hello World From CHelloWorld";
	}	
}
}
  • As shown above this web service class (CHelloWorld) contains a single method called GetHelloWorld() that returns a string.
  • Add another .asmx file to the project.
  • Open the file and modify it as shown below.
using System;
using System.Web.Services;

namespace HelloWorldWS
{
public class CHelloWorldBackup : 
System.Web.Services.WebService
{
	[WebMethod]
	public string GetHelloWorld()
	{
		return "Hello World From CHelloWorldBackup";
	}
}
}
  • This class is similar to previous one but its name is CHelloWorldBackup. Also, it returns different string from GetHelloWorld() method so that you can identify the method call
  • Now, that we have both the web services ready compile the project.

Creating web service client

Let us build a simple web client for our web service.

  • Create a new ASP.NET web application in VS.NET.
  • The application will have a default web form. Before writing any code we need to add a web reference to our web service. Right click on the references node and select Add web reference. Follow the same procedure as you would have while developing normal web services. Adding  a web reference will generate code for proxy web service object.
  • Place a button on the web form and add following code in the Click event of the button:
private void Button1_Click
(object sender, System.EventArgs e)
{
localhost.CHelloWorld proxy=new localhost.CHelloWorld;
Response.Write(proxy.GetHelloWorld());
}
  • Above code shows how you will normally call a web service. The web reference contains information about the location of the web service.
  • If you move the .asmx file after you deploy this client, it is bound to get an error. To avoid such situation, modify above code as shown below:
private void Button1_Click
(object sender, System.EventArgs e)
{
localhost.CHelloWorld proxy=new localhost.CHelloWorld;
proxy.Url="http://localhost/webserviceurlandtimeout
/HelloWorld.asmx";
Response.Write(proxy.GetHelloWorld());
}
  • In above code we have explicitly set Url property of the proxy class to the required .asmx file. You can easily store this URL in <appSettings> section of web.config file and retrieve it at run time. Now, even if you move your web service, all you need to do is change its URL in the web.config.
  • Following code shows this:
private void Button1_Click(object sender, System.EventArgs e)
{
localhost.CHelloWorld proxy=new localhost.CHelloWorld;
proxy.Url=GetURL();
Response.Write(proxy.GetHelloWorld());
}
public string GetURL()
{
   return ConfigurationSettings.AppSettings["webserviceurl"];
}
  • The web.config looks like this:
<appSettings>
<add 
key="webserviceurl" 
value="http://localhost/webserviceurlandtimeout
/HelloWorldBackup.asmx" />
</appSettings>    

Note that in order to work above code correctly, both the web service should have exactly same web method signatures.

I hope you must have got some idea about how to change target web service at run time.

Keep Coding!

READ MORE
...