How to call/Invoke a web service without adding web reference
This something like - anybody would think why I will require to do this thing ever?. I mean calling a web service without adding reference of it. Obviously, to call a web service I would simply add the "Web Reference.." from the Solution Explorer; select the web service and once I am done; I would create the object of the Web Service and use its method - As simple as that.
BUT, recently I came into a bit different scenario which I would like to share. While working on one of my DotNetNuke Solution - I require to have a web service inside my DotNetNuke project (in one of custom module) which is to be consumed by another application. The whole solution was a product which seem to be hosted at the potential customer's side (on their server) - with appropriate configuration. That means, while hosting the DotNetNuke application at the client side - a fresh installation (using Install version) is needed with whatever "Virtual Directory" name they prefer.
Here the problem starts; because every client may like to have different name/url of the application. That means, name of the "Virtual Directory" can be different at each client space. In this case, in another application which is consuming the web service - I need to modify the web reference as per the "Virtual Directory" name of the hosted DotNetNuke solution! That is not desirable. Another scenario to call a web service without adding web reference - I can think of - is to call a method from more then one web services based on some user input (That is - if you want to dynamically call appropriate web service method).
Anyways, I end up with to calling web method without adding web reference.
for the sake of simplicity, Below is the simple default web method "Hello World" which you will find while you add a web service in your solution.
Once you done with the 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 created 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.
To generate this proxy classes you need to generate these proxy classes thru "WSDL.exe" utility from Visual Basic Command Prompt.
Once you are done, notice that WSDL.exe utility has created a proxy class for our Test web service at "C:\Program Files\Microsoft Visual Studio 10.0\VC\WebService.cs
Just grab the class, create a new website and add that proxy class in App_Code folder. 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 whereever you wish it to be.
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)
WebService objService = new WebService();
Response.Write("Output of the Web method call: " + objService.HelloWorld());
First we created object of the proxy class, assigned the url property value from web.config file's appsetting section and call the web method. Thats it!.Hope this would be helpful and informational to somebody who need the same.
Below is the output of our example application:
Conclusion: you can call web service method without adding reference by creating proxy class thru wsdl.exe utility.