![]() |
Languages »
C / C++ Language »
General
Intermediate
How to Configure Web Service Calls from Client Applications at RuntimeBy Het2109A simple and easy-to-understand project on how to configure Web Service calls from client applications at runtime. |
C#, XML, Windows, .NET 1.1, ASP.NET, IIS, VS.NET2003, Dev
|
|
Advanced Search |
|
|
|
||||||||||||||||
In one of my projects, I had to develop a web service and deliver the setup to the client. I faced problems while changing the Target Web Service as the client will install it anywhere in his machine with any name. With this article, I am trying to make it easy to understand for a person who is beginner to Web Services (just like me :-D), how to change the Target Web Service.
One of the most simplest ways of adding a web service to a client application is by adding a web reference to the web service by specifying the URL of the .asmx file. This generates the required proxy object, that's what VS.NET takes care of. 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 a configurable URL so that even if the original web service is moved, your client applications need not be recompiled. In this article, we will see how to do just that.
I will create two web services here, one of which will have direct call and another web service will look into the web.config file for the reference.
For our example, we will develop a simple web service that has only one method. The following steps will show you how to proceed.
using System;
using System.Web.Services;
namespace HWWebService
{
public class HWClass : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello Application, from Web Service";
}
}
}
HWClass) contains a single method called HelloWorld() that returns a string.
using System;
using System.Web.Services;
namespace HWWebService
{
public class AnotherService : System.Web.Services.WebService
{
[WebMethod]public string AnotherHelloWorld()
{
return "Hello World, from Another Service";
}
}
}
AnotherService. Also, it returns a different string from AnotherHelloWorld() method so that you can identify the method call.
Let us build a simple web client for our web service.
txtReturnWS and txtAnotherWS.
tbnWS and btnAnotherWS. Add the following code in the Click event of the button: private void btnWS_Click(object sender, System.EventArgs e)
{
HelloWorld.HWServiceClass objProxy = new HelloWorld.HWServiceClass();
objProxy.Url = GetHWServiceURL();
txtReturnWS.Text = objProxy.HelloWorld();
}
private void btnAnotherWS_Click(object sender, System.EventArgs e)
{
AnotherWorld.AnotherService objProxy = new AnotherWorld.AnotherService();
txtAnotherWS.Text = objProxy.AnotherHelloWorld();
}
Url property of the objProxy class to the required .asmx file.
<appSettings> section of the 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.
The following code shows this:
private void btnWS_Click(object sender, System.EventArgs e)
{
localhost.HWClass objProxy=new localhost.HWClass;
objProxy.Url=GetHWServiceURL();
Response.Write(objProxy.HelloWorld());
}public string GetHWServiceURL()
{
return
System.Configuration.ConfigurationSettings.AppSettings["HWServiceURL"];
}
<appSettings>
<add key="HWServiceURL"
value="http://localhost/HWWebService/HWService.asmx" />
</appSettings>Note that in order for the above code to work correctly, both web services should have exactly same web method signatures.
Hope it will be useful to the team!
Happy dotnetting!
Het Waghela
Het Waghela, Blog | Het Waghela DotNet Questions Link | Het Waghela Resume | More Links
| You must Sign In to use this message board. | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 6 Jun 2005 Editor: Smitha Vijayan |
Copyright 2005 by Het2109 Everything else Copyright © CodeProject, 1999-2009 Web19 | Advertise on the Code Project |