Web services with Language Interoperability

A web service in general is a way of exposing the properties and methods through the Internet In other words, it's an URL-addressable resource that programmatically returns information to clients who want to use it. A web service is based on XML standards and has been implemented by different vendors like IBM, Sun Microsystems, CapeClear etc. Currently there is no official body, such as the W3C who have under taken the job of defining this fast evolving technology.

Web services have been foreseen as the future of computing over the web and are more pronounced in the .NET framework owing to its ease in development as well as consumption with or even without VisualStudio.NET. A webservice works on Simple Object Access Protocol (SOAP). Every call to the webmethod from the client's machine is sent in the form of a SOAP envelope and result from the server is sent back in SOAP containing the result.

The facility of language interoperability that .NET offers within its Common Language Runtime (CLR) offers a new dimension to using web services. Owing to these attractive features, .NET is predicted to take command of the client side computing market in the years to come. We can for sure get a feel on the nature of computing where you could develop and use applications in languages of your choice, at least in .NET environment.

In this article I plan to elaborate more on this aspect by

  • Developing a simple web service in a normal text editor in C#.
  • Consuming the web service described above through a console application written in VB.NET.

Although the application might seem complicated, it is fairly simple to achieve this objective. For the sake of better understanding, I have illustrated simple and straightforward examples.

Let's march in to out first step of our objective. 

Creating a simple Web service in C#

The webservice application provides a Web Method called Add, which takes in two integers as parameters and returns their sum as integer. The following code is typed in a simple text editor. 

<%@WebService Language="c#" class ="sumThis"%>
using System;
using System.Web.Services;
public class sumThis:WebService
{
[WebMethod]
public int Add(int a,int b)
{
int sum;
sum=a+b;
return sum;
}
}

Note: MSDN documents imply that although language interoperability is supported within the .NET platform, it is advisable to use attributes like the ones described below to ensure type compliancy detection at compile time. In other words, these attributes help identify the types that are not recognized by the CTS at compile time. These attributes need to be included in the program prior to declaration of the class in the program. 

//optional attributes to ensure type compliancy
// Assembly marked as compliant.
[assembly: CLSCompliantAttribute(true)]
// Class marked as compliant.
[CLSCompliantAttribute(true)]

Few pointers that one must pay attention to while developing a web service are:

  • The file must be saved as filename.asmx. The file can be checked for its proper syntax and its construct by opening it in a web browser.
  • It is not necessary to compile the web service (.asmx) file.
  • In case of developing a webservice in VB.NET one must use the construct <WebMethod()> instead of the regular construct [WebMethod] used in C#.

1.jpg

View of sumThis webmethod in the browser 

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 

2.jpg

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 

3.jpg

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.  

4.jpg

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:

  1. Professional C# by Simon Robinson et al.
  2. Create and Deploy a web service using C# Rajadurai P (csharphelp.com).
  3. Professional ASP.NET by Richard Anderson et al.
  4. MSDN Documentation from http://msdn.microsoft.com/net.
  5. www.gotdotnet.com
  6. www.webservices.org


Similar Articles