Debugging Code in Web Services

A type of problem that programmers encounter is a program that compiles, but then behaves differently than expected.

Debugging

Debugging is the act of observing the run-time behavior of a program and determining the location of semantic errors. With the debugger, you can break (suspend) execution of your program to examine your code, evaluate and edit variables in your program, view data in database tables and control the execution of the instructions in your source code.

 

You can debug your Web Service function code in several different ways:

 

 

Important!

This section assumes that you have been going through the Quick Start tutorial step by step.

If you skipped the Windows application part of Quick Start, you should read the Handling Compiler Errors section now and return to this page when you have completed it.

 

You are going to make a change in the code of your Greeting Web Service function, to cause it to behave differently than you want it to behave. Then, you will learn how to use the debugger to track down and solve the problem.

  1. Bring up the code for the Greeting function in the Greeting.wo file in Code Explorer.

  2. Change the line

Move "Hello, " to sHello

to

Move "Hello," to sHello

(remove the space following the comma in 'Hello,')

  1. Click on the Run button on the Studio's toolbar. The program will compile and run.

  2. When the application runs, your browser should display the http://localhost/GreetingWebService/GreetingService.wso page. Click the Greeting Web Service link to load the GreetingService.wso page. On this page, click on the Greeting link. Type your name into the sName form and click the Invoke button.

    You will see that the result that appears in the Result form now is, for example, "Hello,John, how are you today?", with the space between the comma and the name missing. You will now use the debugger to track down why the space is missing.

  1. Leave the program running and switch back to the Studio. Click in the left hand margin of the code editor next to the code line

move "Hello," to sHello

  1. This will place a red dot in the left margin, indicating that you have just placed a breakpoint on this line of code.

Breakpoints

Debuggers work by causing executing programs to stop and examining the program at this "frozen" time. Breakpoints on lines of code cause the debugger to halt the program when reaching that line of code, just prior to executing it.

  1. Now you will execute the program up to this line of code to have the debugger stop at this point. Since the breakpoint is in the Greeting function of the oGreeting WSO, you need to run your program and click on the Invoke button to get the debugger to execute the program's code up to this breakpoint.

    Return to the running program and click on the Invoke button again.

    This time, the program is stopped by the debugger at the breakpoint.

  2. If you place your mouse cursor over any local variable in Function Greeting, you will see the current value of the variable pop up as a tool tip. Of course, at this point in the code, all variables should show up to be blank.



    Notice also the green arrow in the left margin, which currently overlaps the red dot for the breakpoint you placed. The green arrow indicates the line on which the program is currently "frozen". This is the next line that will execute in the program.

  3. Take a look at the Locals window. If the Locals window is not open, click on the Locals icon on the Studio's toolbar.



    This will open the Locals window, which lists all local variables and their current values. This is a useful window to keep open when debugging, since it reflects the values of all local variables as you step through the code.

  4. Click on the Step Over button on the Studio's toolbar (or press F10).

Stepping through code

Stepping through code is an important debugging concept. A step causes the debugger to tell the program being debugged to execute lines of code as you tell it to.

  1. The debugger has executed the line

move "Hello," to sHello

Notice that the variable sHello now contains "Hello,". You can see this in the Locals window and also by holding the mouse cursor over the variable sName anywhere inside Function HelloWorld.

  1. Click on the Step Over toolbar button twice. The green arrow should now be next to the line

Function_Return sReply

 



The variable sHello now contains "Hello," sName contains the name you typed into the Name form, sHowAreYou contains ", how are you today?" and sReply contains the concatenated result of sHello + sName + sHowAreYou ("Hello,Dennis, how are you today" in our example images).

It is now readily apparent that the reason for the missing space is that there was no space placed either at the end of sHello or the beginning of sName.

 

Red values in Locals window

You will notice that the value of sReply is displayed in red in the Locals window (yellow if running the Studio in Dark Mode). This is a visual cue to indicate that the value of sReply has just changed.

  1. Take a look at the Watches window. If the Watches window is not open, click on the Watches icon on the Studio's toolbar.



    This will open the Watches window, another useful window to keep open when debugging. The Watches window can display any expression you wish to evaluate while debugging. Try it like this:

  1. Be sure to change "Hello," back to "Hello, ". You will call the Greeting web service function in the Consuming Your First Web Service tutorial.

 

We encourage you to spend some time using the DataFlex Debugger, it is a very powerful tool and the features we have shown you here barely scratch the surface of what you can do with it.

Mastering the Debugger will help you understand how DataFlex works and make you a better programmer.

 

Next Step

You have just learned how to create a web service application and a web service function in the DataFlex Studio, and how to compile and debug them. Next, you will learn how to use web services in DataFlex applications: Consuming Your First Web Service