|
XML Web Services
Sample Service 2
In this section we will create a Calculator Web service that works similar to a Calculator
and performs operations like Add, Subtract, Multiply, Divide and we will consume this
Web service with a Visual Basic Windows Application. To start, open a new project
and select ASP .NET Web service or add a new Web service to the existing ASP .NET
Web service project by right-clicking the project name in Solution Explorer and selecting
Add->Add Web service. Name this project as Calculator, open the code behind file
and start writing the following code.
Imports System
Imports System.Web.Services
<WebService(Namespace := "http://tempuri.org/")> _
Public Class Service1
Inherits System.Web.Services.WebService
#Region " Web Services Designer Generated Code "
#End Region
<WebMethod(Description:="Click to Add numbers")> Public Function Add_
(ByVal x As Integer, ByVal y As Integer) As Integer
'this method adds two numbers by accepting the input from the user
'Description property allows to document the functionality of the Web method.
Return x + y
End Function
<WebMethod(Description:="Click to Subtract numbers")> Public Function Subtract_
(ByVal x As Integer,ByVal y As Integer) As Integer
'this method subtracts by accepting the input from the user
Return x - y
End Function
<WebMethod(Description:="Click to Multiply numbers")> Public Function Multiply_
(ByVal x As Integer,ByVal y As Integer) As Integer
'this method multiplies two numbers by accepting the input from the user
Return x * y
End Function
<WebMethod(Description:="Click to Divide numbers")> Public Function Divide_
(ByVal x As Integer,ByVal y As Integer) As Integer
'this method divides two numbers by accepting the input from the user
If (y = 0) Then
Throw New Exception("Can't divide by zero")
'if number entered is 0 throws an exception
End If
Return x / y
End Function
End Class
|
Once when you finish with the code run the service by selecting Debug->Start from
the main menu or by pressing F5 on the keyboard. The Service that loads can be viewed
by clicking here. You can view all the
methods we created in code along with the method description on that page. Also you
can enter some values in the Textboxes and test the service. We will consume this
service in a Windows Form.
Consuming this Web service
Open a new Visual Basic Project and select Windows Application from the template.
From the toolbox add a Button to the form. Our intention here is to consume the Web
service which we created with this Windows Application. When we click the Button it
will call the method which we specify in it's click event and will return the calculated
result in a MessageBox.
Adding Web Service Reference to the Windows Application
We can add a reference to the Web service in two ways, with Solution Explorer and
using WSDL tool. In the Solution Explorer right click on references and
select Add Web Reference. That opens up a template similar
to the image below.
In the address bar type the URL of the Calculator service which we created. Since
it's in the root directory of IIS you need to type the following address: http://localhost/Calculator/Service1.asmx.
It should look like the image below.
After the Calculator service is loaded, click Add Reference. That adds a reference
to the Calculator service.
To use WSDL tool to add a reference to this Web service, open Visual Studio .NET command
prompt, change the folder in the command prompt to the location where you created
Calculator and type the following:
WSDL "http://localhost/Calculator/Service1.asmx" /l:VB.
After you finish typing the command, in Solution Explorer, right-click Calculator,
select Add and then click Add Existing Item. Locate Service1.vb, and then click to
select it. Click Open.
Calling the Service from Windows Form
Open Form1 and place the following code. Recall that we are calling a method when
the Button in this application is clicked. We need to create an instance of the proxy
class localhost.Service1 and call the function, passing
a string argument. The code for that looks like this:
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
#End Region
Dim myService As localhost.Service1 = New localhost.Service1()
'creating an instance
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As_
System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As_
System.EventArgs) Handles Button1.Click
MessageBox.Show("Sum is " & myService.Add(10, 20))
'calling the Add method in the Web Service returning the result in a messagebox
End Sub
End Class
|
Once you finish with the application, run the form and click on the Button. The sum
of two numbers will be displayed in a MessageBox. We not only created a Web service
but also consumed the service in other application.
|