Introduction
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.
Create web service
For our example, we will develop a simple web service that has only one method. The 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 the following code to it:
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";
}
}
}
- As shown above, this web service class (
HWClass
) contains a single method called HelloWorld()
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 HWWebService
{
public class AnotherService : System.Web.Services.WebService
{
[WebMethod]public string AnotherHelloWorld()
{
return "Hello World, from Another Service";
}
}
}
- This class is similar to the previous one but its name is
AnotherService
. Also, it returns a different string from AnotherHelloWorld()
method so that you can identify the method call.
- Now that we have both the web services ready, compile the project.
Creating the 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 the proxy web service object.
- Place two text boxes. Name them as
txtReturnWS
and txtAnotherWS
.
- Place two buttons on the web form and name them
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();
}
- The 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 a situation, modify the above code as shown below:
private void btnAnotherWS_Click(object sender, System.EventArgs e)
{
AnotherWorld.AnotherService objProxy = new AnotherWorld.AnotherService();
txtAnotherWS.Text = objProxy.AnotherHelloWorld();
}
- In above code, we have explicitly set the
Url
property of the objProxy
class to the required .asmx file.
- You can store this URL in the
<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.
- You can add the URL of the webservice in the web.config file in two ways:
- directly by adding the required tag to the web.config file and
- by right-clicking the service used in the application, set its property to dynamic. This will automatically add the tag required for the service, you can change the name of the service to "HWServiceURL".
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"];
}
- The web.config looks like this:
<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