अजपा योग क्रिया आणि ध्यान : श्वास, मंत्र, मुद्रा आणि ध्यान यांच्या सहाय्याने मनःशांती, एकाग्रता, चक्र संतुलन आणि कुंडलिनी जागृती. अधिक माहिती आणि आगामी तारखांसाठी येथे जा.


<%@ Page %>

Changing Target Web Service At Runtime

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!


Bipin Joshi is an independent software consultant and trainer by profession specializing in Microsoft web development technologies. Having embraced the Yoga way of life he is also a meditation teacher and spiritual guide to his students. He is a prolific author and writes regularly about software development and yoga on his websites. He is programming, meditating, writing, and teaching for over 27 years. To know more about his ASP.NET online courses go here. More details about his Ajapa Japa and Shambhavi Mudra online course are available here.

Posted On : 05 April 2002