• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL

Chapter 30. Web Services > Building a Simple Calculator Web Service

Building a Simple Calculator Web Service

Now that you have a basic understanding of web services, what they do, what they are composed of, and how to implement them using Dreamweaver, let's build a web service that performs a simple calculation of two numbers. After the simple web service has been created, we'll review how to consume the web service using both ASP.NET and ColdFusion server models. You can begin creating your web service by following these steps:

Start by creating a new folder in C:\Inetpub\wwwroot called CalculateService.

In Dreamweaver, create a new document by choosing the New option from the File menu. Choose the ASP.NET VB option from the Dynamic Page category and click Create.


Remember that web services are operating system agnostic and server technology independent. For this reason, you can create the web service with VB.NET and have it be consumed using ASP.NET VB, ASP.NET C#, or ColdFusion. For the sake of simplicity, we'll create the web service using VB.NET but consume it using both ASP.NET and ColdFusion.

Immediately save your file as calculate.asmx, saving it to the newly created folder C:\Inetpub\wwwroot\CalculateService. (Note that .ASMX is the extension given to ASP.NET web services.)

Switch to Code view, remove all existing code, and add the following code to the page in its place:

<%@ Webservice class="CalculateService" %>
Imports System.Web.Services
Public Class CalculateService
Inherits System.Web.Services.WebService
<WebMethod()> Public Function Calculate(x As Integer, y As Integer) As String
     Return x + y
End Function
End Class


The code should include the Webservice directive, the System.Web.Services namespace import, and the CalculateService class that defines the Calculate function. Notice that the function is distinguished as a web method by the special <WebMethod()> identifier. The function accepts two parameters (X and Y) as integers and performs a simple addition on them, returning the value as a string. Save your work.



Not a subscriber?

Start A Free Trial

  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint