Why would anybody want to do this since there is a simple way given by Visual studio by adding a Web Reference from Solution Explorer. But recently wants to build an application which should install in different client locations with different names etc. As in the installer we can give different custom names to virtual directory this will not work for me. That means “Virtual Directory” can be different for each client in that case i need modify the web reference as per the “Virtual Directory” name of the hosted solution.
So for that i end up adding proxy class of the web service in my solution by using WSDL tool. I need to generated Proxy class of the web service that i have created and add the proxy class in my Web solution. For example here is a sample web service code for simplicity
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace SampleWebSerivce
{
/// <summary>
/// Summary description for Service1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
}
}
Once your done with your custom web methods, you add the web reference where you actually consume the web service methods. When you add a web reference - based on the WSDL specifications IDE actually creates proxy classes in your application which represents the web service classes and its methods on the remote server
Visual Studio provides Intellisense with Web services, which will not be accurate till you update your web reference each time you change your web service, it is because the proxy classes which are local to your solution are being used to offer you the Intellisense. So, ultimately when you invoke a Web method you are invoking a method in one of these proxy classes. These classes, in turn, are capable of invoking the remote methods on the Web Service URL.
So, every time the web service is updated, these proxy classes need to be regenerated by clicking Update Web Reference. However, the proxy classes do not always have to be generated from the IDE, .NET also gives us the WSDL.exe utility to manually generate these proxy classes and include them in your project.
Here is the syntax to create Proxy class from WSDL tool.
Just grab the class, and add in the App_Code folder if its a web site or you can add in root folder if it is a web solution.
If you open the proxy class you will notice the constructor of the proxy class contains "url" member property which is by default assigned the url of the website in which the web service exists. You can store the web service url in web.config or in database or as per your logic.
So, finally to call the "Hello World" web method - you need to create object of this proxy class and assign the url property correctly as below:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Service1 objService = new Service1();
objService.Url =
System.Configuration.ConfigurationManager.AppSettings["WebServiceUrl"];
Response.Write("Output of Web method call: " + objService.HelloWorld());
}
}
Finally, You can call web service method without adding Web Reference. But make sure you update the proxy class through WSDL utility as and when you have any changes in the Web Service.