If we were to use the webservice in a client's machine, we need to create a proxy class or more commonly called stub file. The proxy class contains the details of the web method, the server it is contained in etc. This proxy class can be generated on the client's machine by using the utility WSDL.exe which is packaged along with .NET SDK (Beta 2).
Consuming a Web service in a Client's machine using VB.NET
One of the highlights of this utility is one can generate the stub file by just specifying the ip address or the hostname that contains the web service. At the command prompt, the stub file can be generated as
WSDL http://[hostname of server] or [ip address of server] [/n:[namespace to be used in client's machine]] [/l: [language of proxy class=VB/C#]] /out:[output file name[.cs/.vb]]
My compilation string for the webmethod sumThis was
wsdl http://ora08.st.utoledo.edu/vijay/webservice/addService.asmx /n:myNamespace /l:vb /out:vbAddWebservice.vb
There are other switches in the command which can be explored by issuing
WSDL /? at command prompt. We can inspect the output file for more details on the WebMethod.
Once the output file is obtained, we have to compile it using appropriate compiler to produce a DLL file (in my case the VB.NET's compiler).
vbc /t:library /r:System.dll,System.web.services.dll,System.xml.dll vbAddWebService.vb
Note: The VB.NET compiler needs to be given the reference of System.dll too, which is not so in the case of C# compiler.
Now we are all set to create a client's application that can access the webmethod Add defined in the webservice. In this example, the client application I conceived of is a simple console application written in VB.NET. The code for the application is as follows:
Imports System
'import the namespace assigned in WSDL compilation
Imports myNamespace
Module Module1
Sub Main()
'instantiating a object of the webservice
Dim ob As New sumThis
'Ask the user to input two value to add up
Console.WriteLine("***ENTER TWO INTEGERS TO ADD***")
Console.Write("FIRST INTERGER :")
'inputing the values in variables
Dim var1, var2 As Integer
var1 = Integer.Parse(Console.ReadLine())
Console.Write("SECOND INTERGER :")
var2 = Integer.Parse(Console.ReadLine())
'Obtain the answer and display
Console.WriteLine("The Sum of {0} and {1} is {2}" var1, var2,ob.Add(var1,var2))
End Sub
End Module
Compile the above application at the command prompt as
vbc /r:vbAddWebservice.dll,system.dll,system.web.services.dll,system.xml.dll testerCalc.vb
Test the application as a normal console application. And voila, we have an application that can communicate to a method from another server written in language different than that of the client's development language, albeit on the same platform.
One can similarly develop and use web services with ease in Visual Studio .NET. This example was intended to make clear that web services could be used in simple console and other command line utilities with ease as any other application.
References:
- Professional C# by Simon Robinson et al.
- Create and Deploy a web service using C# Rajadurai P (csharphelp.com).
- Professional ASP.NET by Richard Anderson et al.
- MSDN Documentation from http://msdn.microsoft.com/net.
- www.gotdotnet.com
- www.webservices.org