Embrace Cross-Platform Interoperability with Web Services

by Nitin Gupta

Introduction

Create a .NET* Web service and then write a Java*-based client to invoke it with this code and step-by-step procedures.

Enterprise infrastructures have grown to become a mixture of heterogeneous technologies and platforms due to various influences, such as rapidly changing technologies, decreasing hardware costs, and changing user requirements. Enterprise computing has moved away from mainframe-based computing to become more dependent on personal computers. In this environment, application data must be provided to users with various client platforms and to partners with diverse IT infrastructure.

Enterprises have struggled over the years to bring together these different technologies and to provide a layer of interoperability that can allow this information to be accessed transparently, regardless of underlying operating systems, hardware, software technologies, and networking infrastructure.

Some past attempts to resolve this issue, such as DCOM* and CORBA*, have met with limited success due to a lack of vendor support, firewall/security issues, and the use of proprietary technologies.

This article targets developers who have some exposure to both the .NET and Java programming frameworks and a basic understanding of Web services. It walks through developing the solution to reveal how these two diverse environments can work together seamlessly. This exercise helps developers to understand the issues they might face during development and to appreciate the simplicity of the opportunities that this interoperability provides to the enterprise today.


Web Services Are Cross-Platform by Definition

Web services promise to resolve interoperability issues forever, and this technology is available now. This solution is based on providing interoperability between various enterprise development environments, mainly .NET and J2EE*, using standard protocols and technologies like HTTP, XML, and SOAP that are widely used in the Internet.

The emergence of these technologies as a means of communication between clients and service providers finally allows both parties to operate in a truly platform-independent manner. Since these standards are open, non-proprietary, and freely available, they are being readily endorsed by different vendors, which have begun providing enterprise solutions around these technologies. Some of the standards, like SOAP, are still being refined through an open-community process that includes different vendors and stakeholders.

The use of XML, SOAP, and HTTP provides a neutral "language" for heterogeneous enterprise environments to communicate with each other. This allows for easy interoperability, where a Web service can be accessed transparently by client applications written in various languages, on any operating system, and running on myriad devices. The client might be on an end user's personal computer, a handheld device, or a particularly smart wristwatch, or it might be an application owned by an enterprise partner.


Sample Solution Pairs a .NET Web Service and a Java Client

The sample solution in this series of articles illustrates how to obtain interoperability between .NET and Java environments. In this article, a .NET-based Web service is developed. In the next article, we will develop a client using Java to consume that service. This solution can also be implemented the other way around (as a Java-based Web service and a .NET-based client), but that is left as an exercise for the reader to implement.

The solution consists of a Web service that provides a user's phone number and a Java-based client with a user interface that will obtain that information as a consumer for the Web service. These two will communicate transparently using SOAP and HTTP.



We are going to implement a Web service that provides phone book functionality. This service implements a method called "GetPhoneNumber" that will return the phone number of a person after looking it up in a database. This method will take two input parameters: the person's first name and last name.

This Web service will be written using Visual Basic .NET*, and it will use ADO.NET* to connect to a SQL* server and to execute SQL queries to look up the user information in the database. In particular, we will use the "Northwind" sample database that comes installed on SQL servers by default. This database has a table called "Employees" that has three columns – FirstName, LastName, and PhoneNumber – which we will use for our SQL-based queries.


Create a .NET-Based Web Service

In the .NET environment, a Web service can be developed using VisualStudio .NET* and can be written in multiple languages, including C#* and Visual Basic .NET. Use of Visual Studio .NET is warranted for development of complicated Web services, but a simple text editor will suffice to create the very simple Web service in this article. For those who prefer it, Visual Studio .NET will also work very well for these examples.

Before you start, please download and install the .NET Framework* if you do not already have it installed. The runtime environment will be automatically added to IIS and will allow you to run a .NET-based Web service on your system.

Create a directory called "phonebook" in the IIS wwwroot directory. Usually this directory is "c:Inetpubwwwroot". Next, use a text editor to write the following code and save the file as "GetPhoneInfo.asmx" in this new "phonebook" directory:

<%@ WebService Language="VB" Class="PhoneService"

%>


Imports System.Web.Services

Imports Microsoft.VisualBasic

Imports System.Data.SqlClient


Public Class PhoneService Inherits

System.Web.Services.WebService

<WebMethod()> Public Function

GetPhoneNumber ( ByVal strLastName As String, ByVal

strFirstName As String ) As String


Dim strConn As String =

"Pooling=false;server=dbserver01;Database=Northwind;uid=testuser;Password=password;"

Dim SQLConn As New

SqlConnection(strConn)

Dim strSQLQuery As String =

"select HomePhone from Employees where FirstName Like '"

& strFirstName & "' And 

LastName Like '" & strLastName

& "'" Dim SQLCmd As New

SqlCommand(strSQLQuery, SQLConn)


On Error Resume Next


SQLConn.Open()


If Err.Number <> 0

Then

return

"Error connecting to SQL server: " & strConn

& " " & Err.Number

& ". Desc: " &

Err.Description

End If


'-------------- execute

query

Dim myReader As

SqlDataReader =

SQLCmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)


While myReader.Read()

GetPhoneNumber=(myReader.GetString(0))

End While


If Err.Number <> 0

Then

return

"Error executing SQL query: " & Err.Number

& ". Desc: " & 

Err.Description

End If


myReader.Close()

End Function

End Class

 

Here is a summary of what is happening in the code above:

The first line identifies this class as a Web service. Next, we import the required namespaces that contain the relevant classes for the Web service. Our class called "PhoneService" inherits "System.Web.Services.Webservice."

Define a method called "getPhoneNumber" that will expose the desired functionality of the Web service. Please note the "<WebMethod>" prefix. In this method, the next lines of code open a connection to the SQL* database, "Northwind," and run our SQL queries against the table, "Employees." Use the appropriate SQL server name, user ID, and password with the right privileges.

The code performs a query for an employee record based on first and last names, and subsequently returns the phone number as a result. Errors result in an error string being returned instead of the phone number. This error handling is rudimentary, but it serves our purposes here.


Test the .NET-Based Web Service

To test the newly created Web service, access it using a Web browser by typing the URL that points to the "GetPhoneInfo.asmx" file:


Click here for a larger image.

Now click on the "GetPhoneNumber" link at the top to access this new page:



To test the Web service, enter the last name as "King" and first name as "Robert" and click the "Invoke" button (this name was chosen because we know that it is present in the sample database "Northwind" we are using).

A new browser window will open up and will display the phone number as shown below:


Conclusion

At this stage, we have a complete Web service, developed in the .NET environment, up and running. And note: we could have developed this Web service using Java based-APIs. This again demonstrates the cross-platform strength of Web services. In the next article, we will create a Web services client using Java-based libraries.


Additional Resources

Intel, the world's largest chip maker, also provides an array of value-added products and information to software developers:

  • Intel® Software Partner home provides software vendors with Intel's latest technologies, helping member companies to improve product lines and grow market share.
  • Intel® Developer Zone offers free articles and training to help software developers maximize code performance and minimize time and effort.
  • Through a series of white papers, case studies and other materials, IT@Intel describes the lessons it has learned in identifying, evaluating, and deploying new technologies: http://www.intel.com/it/

 


About the Author



Nitin Gupta holds a masters degree in computer science from the University of Southern California. Nitin has over six years of programming and system design experience. He welcomes your comments at nitgupta@alumni.usc.edu.


 

 

Web Series Articles by Nitin Gupta

 


For more complete information about compiler optimizations, see our Optimization Notice.
Categories: