Hi,
This is sample only
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:\Inetpub\wwwroot". 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.
The first phase to developing a Java-based client is to generate the client-side stubs for the Web service we are interested in. We can do that by using a tool provided in the Java WSDP called xrpcc. This tool is available in the <jwsdp_install_dir>\bin directory, and it can generate client-side stubs that internally use JAX-RPC to communicate with the Web service. To use this tool, follow this procedure:
Download and save the Web Services Description Language (WSDL) file for the PhoneService Web service as GetPhoneInfo.xml. You can retrieve the WSDL file for the service we have written by accessing this URL (assuming we are using .NET based Web service running on IIS): http://localhost/phonebook/GetPhoneInfo.asmx?wsdl
The xrpcc tool does not expect service definition files as input; instead, it expects as input the XML configuration file with information about the WSDL file. Now create a file called config.xml that has the following format: <?xml version="1.0" encoding="UTF-8"?>
<configuration
xmlns="http://java.sun.com/xml/ns/jax-rpc/ri/config">
<wsdl location="GetPhoneInfo.xml"
packageName="phonepackage">
</wsdl>
</configuration>
For the WSDL model, the configuration file specifies two pieces of information:
Line 4 refers to the WSDL file for the PhoneService Web service for which we are developing the client (which we saved earlier). Alternatively, we may decide to specify the location as a URL, which in our case is the following: http://localhost/phonebook/GetPhoneInfo.asmx?wsdl
Line 5 refers to the fully qualified package name to which the client Java classes will belong.
Run the tool with the following command line: C:\source>xrpcc -client -verbose config.xml
You will see the following output:
warning: ignoring port "PhoneServiceHttpGet": no SOAP address specified
warning: ignoring port "PhoneServiceHttpPost": no SOAP address specified
[ServiceInterfaceGenerator: creating service interface: phonepackage.PhoneService]
[ServiceGenerator: creating service: phonepackage.PhoneService_Impl]
[SerializerRegistryGenerator: creating serializer registry:
phonepackage.PhoneService_SerializerRegistry]
[CustomClassGenerator: generating JavaClass for: GetPhoneNumber]
[CustomClassGenerator: wrote file: C:\source\.\phonepackage\GetPhoneNumber.java]
[CustomClassGenerator: generating JavaClass for: GetPhoneNumberResponse]
[CustomClassGenerator: wrote file:
C:\source\.\phonepackage\GetPhoneNumberResponse.java]
[LiteralObjectSerializerGenerator: writing serializer/deserializer for:
GetPhoneNumber]
[LiteralObjectSerializerGenerator: wrote file:
C:\source\.\phonepackage\GetPhoneNumber_LiteralSerializer.java]
[LiteralObjectSerializerGenerator: writing serializer/deserializer for:
GetPhoneNumberResponse]
[LiteralObjectSerializerGenerator: wrote file:
C:\source\.\phonepackage\GetPhoneNumberResponse_LiteralSerializer.java]
[done in 7922 ms]
|