Debugging

Debugging is the process of finding the causes of errors in a program, locating the lines of code causing those errors, and fixing the errors.

To enable debugging in an ASP.NET application, you need to ensure that the debug attribute of the <compilation> element in the web.config file is set to true , like so:

 <compilation debug="true"/> 
graphics/note_icon.gif

When you deploy your application, be sure that you change the debug attribute in the <compilation> element in the web.config file to false . This will improve the application's performance.


Setting Breakpoints and Stepping Through Program Execution

Breakpoints are markers in the code that signal the debugger to pause execution. After the debugger pauses at a breakpoint, you can analyze variables , data records, and other settings in the environment to determine the state of the program.

Follow these steps to learn how to set breakpoints and perform step-by-step execution of a program:

  1. Create a new Web application project ( Example13_3 ) with the same user interface as Example13_1 .

  2. Switch to code view and add the following using directive:

     using System.Diagnostics; 
  3. Add the following method to the class:

     private int Factorial(int intNumber) {     int intFac = 1;     for (int i = 2; i <= intNumber; i++)     {         intFac = intFac * i;     }     return intFac; } 
  4. Modify the Click event handler of btnCalculate to the following:

     private void btnCalculate_Click(object sender, System.EventArgs e) {     int intNumber, intFac;     try     {         intNumber = Convert.ToInt32(txtNumber.Text);         intFac = Factorial(intNumber);         lblResult.Text = String.Format             ("The factorial of {0} is {1}", intNumber, intFac);     }     catch(Exception ex)     {         Debug.WriteLine(ex.Message);     } } 
  5. In the event handler added in step 4, right-click the beginning of the line that makes a call to the Factorial() method and select Insert Breakpoint from the context menu. You will note that the line of code is highlighted with red and also that a red dot appears on the left margin. You can alternatively create a breakpoint by clicking the left margin adjacent to a line.

  6. Set the project as the startup project for the solution and run the project. Enter a value and click the Calculate Factorial button. Note that execution pauses at the location where you have marked the breakpoint. You should see an arrow on the left margin of the code indicating the next statement to be executed.

  7. Select Debug, Step into to step into the code of the Factorial() method. Hover the cursor over the various varibles in the Factorial() method; you'll see the current values of these variables.

  8. Select Debug, Step Out to automatically execute the rest of the Factorial() method and restart the step mode in the event handler at the next statement. Step through the execution until you see the form again.

graphics/alert_icon.gif

Breakpoints and other debugging features are available only when you run your project using the Debug configuration.


To set advanced options in a breakpoint, you can choose to create a new breakpoint by selecting the New Breakpoint option from the context menu of the code or from the toolbar in the Breakpoints window. The New Breakpoint dialog box, shown in Figure 13.1, has four tabs. You can use these tabs to set a breakpoint in a method, in a file, at an address in the object code, and when a data value (that is, the value of a variable) changes.

Figure 13.1. The New Breakpoint dialog box enables you to create a new breakpoint.

graphics/13fig01.jpg

Clicking the Condition button opens the Breakpoint Condition dialog box, as shown in Figure 13.2.

Figure 13.2. The Breakpoint Condition dialog box enables you to set a breakpoint based on the runtime value of an expression.

graphics/13fig02.jpg

Clicking the Hit Count button in the New Breakpoint dialog box opens the Breakpoint Hit Count dialog box, shown in Figure 13.3. This dialog box can be especially helpful if you have a breakpoint inside a lengthy loop and want to step-execute the program only near the end of the loop.

Figure 13.3. The Breakpoint Hit Count dialog box enables you to break the program execution only if the specified breakpoint has been hit a given number of times.

graphics/13fig03.jpg

Analyzing Program State to Resolve Errors

When you break the execution of a program, the program is at a particular state in its execution cycle. You can use debugging windows to help you identify the cause of the error you are debugging. Table 13.5 lists the important debugging windows in Visual Studio .NET.

Table 13.5. Important Debugging Windows in Visual Studio .NET

Window

Description

Call Stack

Enables you to view the names of methods on the call stack, the parameter types, and their values

This

Enables you to examine the members associated with the current object

Autos

Displays the variables used in the current statement and the previous statement

Locals

Displays the variables local to the current method under execution

Watch

Enables you to evaluate variables and expressions

graphics/alert_icon.gif

You can also perform step-by-step execution of SQL Server Stored Procedures in Visual Studio .NET. To configure a project for SQL Server debugging, access the project properties window. Select the configuration properties for debugging; then in the right pane, under the Debuggers node, select True for Enable SQL Debugging.


Debugging on Exceptions

You can control the way the debugger behaves when it encounters a line of code that throws an exception. You do this through the Exceptions dialog box, which is invoked by selecting Debug, Exceptions.

The two levels at which you can control the behavior of the debugger when it encounters exceptions are as follows :

  • When the exception is thrown You can instruct the debugger to either continue or break the execution of the program when an exception is thrown.

  • If the exception is not handled If the program you are debugging fails to handle an exception, you can instruct the debugger to either ignore it and continue or break the execution of the program.

Debugging a Running Process

Visual Studio .NET also enables you to debug the processes running outside the Visual Studio .NET debugging environment. This feature can be helpful for debugging already deployed applications.

When a Web page is requested from the Web server, the ASP.NET worker process ( aspnet_wp.exe ) serves the request. To debug a running page, you need to attach the Visual Studio .NET debugger to the aspnet_wp.exe process running on the Web server. (You can do so in Visual Studio .NET by selecting Tools, Debug Processes.) In addition, you need to open the source files for the Web page in Visual Studio .NET and set a breakpoint in it at the desired location. After this debugging setup is complete, when you interact with the already running Web page, it will break into the debugger whenever the breakpoint is hit.

graphics/note_icon.gif

When you attach a debugger to the ASP.NET worker process aspnet_wp.exe , it freezes the execution of all the other Web applications on that server. This can cause undesirable effects on a production server.


Debugging a Remote Process

The process of debugging a remote process is almost the same as debugging an already running process. The only difference is that, prior to selecting a running process from the Processes dialog box, you need to select the remote machine name . (You can invoke the Processes dialog box by selecting Tools, Debug Processes.)

Before you can remote debug processes, you need to perform a one-time configuration of the remote machine on which the processes are running. To do so, you can use one of the following methods:

  • Install Visual Studio .NET on the remote machine.

  • Install the Remote Components Setup on the remote machine. (This can be installed from the Visual Studio .NET Setup Disc 1.)

These methods install the Machine Debug Manager ( mdm.exe ), which runs as a background service and provides remote debugging support. In addition, these methods add the currently logged-on user to the Debugger Users group; a user must be a member of Debugger Users group to debug. If you want to debug the ASP.NET worker process, you must also have administrative privileges (that is, you should be a member of the Administrators group ) on the remote machine.

graphics/alert_icon.gif

If you get a DCOM configuration error while debugging, you might not be a member of the Debugger Users group on the remote machine. The local computer and the remote computer must be members of a trusted domain for remote debugging to be possible.


For a different configuration or requirement, you refer to the "Setting Up Remote Debugging" topic in the Visual Studio .NET Combined Help Collection.

Debugging the Code in DLL Files

The process of debugging a DLL file is similar to debugging a Web form. There is one difference, though; the code in a DLL file cannot be directly invoked, so you need to have a Web form that calls various methods from the DLL files.

You typically need to perform the following steps for debugging code in a DLL file:

  1. Launch the Web form that uses the methods in the DLL file.

  2. Launch Visual Studio .NET and attach the debugger on the Web form. Set a breakpoint where the method in the DLL file is called; then continue with the execution.

  3. The execution breaks when the breakpoint is reached. At this point, select Debug, Step into to step into the source code of the DLL file. Execute the code in the DLL file in Step mode while you watch the value of its variables.

Debugging Client-Side Scripts

Visual Studio .NET also enables you to debug client-side scripts. The process is similar to the process discussed earlier for ASP.NET Web forms. However, you must note the following points for client-side scripting:

  • Client-side debugging works only with Microsoft Internet Explorer.

  • You have to enable script debugging in Internet Explorer. To do this, select Tools, Internet Options. Then select the Advanced tab and uncheck the Disable Script Debugging option in the Browsing section.

  • Attach the debugger to the iexplore.exe process displaying the Web form. This is required only if you are debugging an already running process. While attaching the process, in the Attach to Process dialog box, select the Script option.

graphics/alert_icon.gif

To configure a project for debugging ASP code, access the project properties window. Select the configuration properties for debugging; then in the right pane, under the Debuggers node, select True for Enable ASP Debugging.




MCAD Developing and Implementing Web Applications with Visual C#. NET and Visual Studio. NET (Exam [... ]am 2)
MCAD Developing and Implementing Web Applications with Visual C#. NET and Visual Studio. NET (Exam [... ]am 2)
ISBN: 789729016
EAN: N/A
Year: 2005
Pages: 191

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net