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"/>
Setting Breakpoints and Stepping Through Program ExecutionBreakpoints 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:
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.
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.
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.
Analyzing Program State to Resolve ErrorsWhen 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
Debugging on ExceptionsYou 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 :
Debugging a Running ProcessVisual 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.
Debugging a Remote ProcessThe 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:
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.
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 FilesThe 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:
Debugging Client-Side ScriptsVisual 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:
|