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

Chapter 29. XML Web Services > A Simple Calculator Web Service

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. You can begin creating your Web service by following these steps:

Open a new ASP.NET Visual Basic document in Dreamweaver and switch to code view.

Immediately save your file as sample.asmx. .ASMX is the extension given to Web services.

Add the following code:

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

The code should include the Web service directive, the Web.Services namespace import, and the Service1 class that defines the Calculate function. Notice that the function is distinguished as a Web method by the special <WebMethod()> tag. The function accepts two parameters (X and Y) as integers and performs a simple addition on them. The result will look similar to Figure 29.2 within Dreamweaver. Save your work.

Figure 29.2. Add the necessary code to the page.

With the Web service now created, you can launch the browser to test its functionality. Navigate to the folder where your Web service is located and find the .ASMX file. The service screen shows you a list of all available Web methods. Figure 29.3 displays the only method within your Web service: Calculate.

Figure 29.3. The service contains just one method—Calculate.

Obviously, you never wrote code that resembles what you are seeing. This screen is a service of the .NET Framework and is available with its installation for use in testing Web services.

Selecting Calculate redirects you to a new screen that enables you to input the X and Y values that the function will accept and perform the calculation on. Click the Invoke button, as shown in Figure 29.4.

Figure 29.4. Click the Invoke button to invoke the function.

The result of the calculation is shown in XML format similar to that of Figure 29.5.

Figure 29.5. The result of the calculation is shown in XML format.

Obviously this does no one any good if it's not applicable in a programmed environment. You can generate a proxy class that you will be able to program against within your ASP.NET pages. This allows for customization of Web controls that invoke and return values for the Web service. Begin the process of generating the proxy class by opening the Command window. Select Start, Run, and type “Cmd”. The Command window opens.

Switch to the .NET Framework SDK's Bin directory. This is where the utility (WSDL.exe) is located and that you will use. This can be accomplished by typing in the following command from the (C:\) prompt:

cd C:\Program Files\Microsoft.NET\SDK\v1.X\Bin

Now type the following line to generate the new VB class file:

wsdl /l:vb http://localhost/WebStore/sample.asmx?WSDL /n:AppSettings

This line of code effectively runs the WSDL.exe utility. Another service of the .NET Framework SDK, the WSDL utility is used to generate class files in either Visual Basic or C#.

On that note, look at the second switch: /l:vb. This switch tells the WSDL utility to make the output class file a Visual Basic file. The next switch simply tells the WSDL utility where the Web service is located. The result is shown in Figure 29.6.

Figure 29.6. Generate the new proxy class.

It is important to note that this line of code effectively creates the new Visual Basic proxy class. In the next step, we'll take that class and compile it into a programmable assembly.


It's important to note that your new class file is created within the Bin folder of the .NET Framework SDK's directory. Later, after you've created the proxy, you'll want to move these files over to the Web Store's Bin directory.

Now we'll need to compile the Visual Basic class file into an assembly DLL by typing in the following code:

vbc /out:Service1.dll /t:library /r:system.Web.dll,
system.data.dll Service1.vb

In this case, we use the VBC executable. VBC stands for Visual Basic Compiler. To compile our class file into programmable DLL, we use the VBC and use three switches. The first, /out:Service1.dll tells the compiler what to name the output .DLL file. The second switch tells the compiler to use a Library assembly (common with Web services). The last switch tells the compiler which classes to include in the DLL file. The result is shown in Figure 29.7.

Figure 29.7. Generate the new assembly.

Notice that each time you run the code, the new file is created within the folder that contains the original .ASMX file.

Now open Dreamweaver and create a new ASP.NET page. Make sure that the assembly (Service1.dll) is located within the Bin directory of the project you are working with. If it isn't, move it there now.

Switch to the Components tab and select the plus sign (+) icon.

Select Add Using Proxy Classes as shown in Figure 29.8.

Figure 29.8. Add the component.

Browse to the location of the assembly as shown in Figure 29.9. Make sure that the .NET DLL Reader is selected.

Figure 29.9. Find the assembly.

Select OK. The Web service is shown within the Components panel. You can view the Calculate function by expanding the service.

Add two new text box controls, one button control, and a label control as shown in Figure 29.10.

Figure 29.10. Add new controls to handle the user calculation values.

Now create a new code declaration block (<script> block) within the <head> tag. Create a new subroutine called Calculate and drag the service followed by the Calculate function within the subroutine. Code similar to Figure 29.11 should be created for you.

Figure 29.11. Drag the object and method into the Code window.

Now add the text value parameters, the code to set the label, and an onClick event to the button, as shown in Figure 29.12.

Figure 29.12. Add the appropriate code to complete the subroutine.

Save your work as calculate.aspx. Before you test your work, copy the service1.dll file into the Bin directory. Now test it in the browser. The result is shown in Figure 29.13.

Figure 29.13. Test the ASPX file within the browser. Set values and invoke the method passing in the parameters to return a result.



Not a subscriber?

Start A Free Trial

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