top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Changing Target Web Service At Runtime

0 votes
258 views

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!

posted Jan 4, 2017 by Shivaranjini

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


Related Articles

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.

READ MORE

Introduction

While working with web services one of the commonly faced question is - How do I secure my web service? One aspect of security is preventing anonymous access to the web service. This can be done in various ways such as passing user id and password with each web method call. One elegant alternative is to use SOAP headers to pass this authentication information. This code sample shows how to use SOAP headers to pass authentication information to the web service.

About SOAP Headers

Every SOAP message consists of SOAP body and optional header. SOAP header serve similar purpose as familiar HTTP headers such as passing some information. Note that SOAP headers are passed as plain text over the network. This means that if you need strong security measures you may not like to use them without some kind of encryption.

About Sample Source Code

A ZIP file is provided with this article for download. The ZIP contains two VS.NET projects - web service and web service client. The client passes user information such as User ID and Password in the SOAP headers to the web service. The web service then authenticates the user and sends back response accordingly.

READ MORE

What is Web Service?

Web services is a standardized way of integrating Web-based applications using the XML, SOAP, WSDL and UDDI open standards over an Internet protocol backbone.

Web services are client and server applications that communicate over the World Wide Web’s (WWW) HyperText Transfer Protocol (HTTP).

A web service is any piece of software that makes itself available over the internet and uses a standardized XML messaging system. XML is used to encode all communications to a web service.

Web services are self-contained, modular, distributed, dynamic applications that can be described, published, located, or invoked over the network to create products, processes, and supply chains.

These applications can be local, distributed, or web-based. Web services are built on top of open standards such as TCP/IP, HTTP, Java, HTML, and XML.

Web services are XML-based information exchange systems that use the Internet for direct application-to-application interaction.

A web service is a collection of open protocols and standards used for exchanging data between applications or systems.

Video for Web Services

https://www.youtube.com/watch?v=u80uPzhFYvc

READ MORE
...