Export (0) Print
Expand All
5 out of 9 rated this helpful - Rate this topic

Debugging Preparation: Class Libraries

Visual Studio .NET 2003

The Class Library project template creates a DLL. For more information, see Class Library Template. Because the class library is a DLL, you cannot run it directly. It must be called by an application (usually an EXE). The calling application may be built in another project in the same Visual Studio solution that contains the DLL, it may be an existing program already deployed on a test or production machine, or it may be located on the Web and accessed through a URL.

This topic contains the following sections, which provide considerations on preparing to debug class libraries:

The Calling Application

To debug a class library, you must first start debugging the calling application. There are three ways to do this:

  • You can open the Class Library project, enter the name and location of the calling application in the <Project> Property Pages dialog box (see instructions below), then start execution from the Debug menu. This will launch the specified executable. For more information, see Starting Execution.
  • If you have a project for the calling application, you can open that project and start execution from the Debug menu. For more information, see Starting Execution.
  • If the calling application is already running, you can attach to it. (Use this method if the class library is a control hosted by Internet Explorer.) For more information, see Attaching to a Running Program or Multiple Programs.

Before you start debugging the calling application, you will usually want to set a breakpoint in the class library. When the breakpoint is hit, you can step through the code, observing the action at each line, until you isolate the problem. For more information, see Breakpoints and Stepping.

Building a Debug Version

No matter how you start debugging, build the Debug version of the class library first and make sure the Debug version is in the location where the application expects to find it. This may seem obvious, but if you forget this step, the application may find a different version of the class library and load it. The program will then continue to run and never hit your breakpoint.

The Class Library Project

To start debugging from the Class Library project (rather than starting from the calling application), you must enter some information in the <Project> Property Pages dialog box. This information tells the Class Library project how to find and invoke the calling application. Select and follow one of the following procedures.

Invoking a Calling Application that is an Existing Program

To invoke a calling application that is an existing program (C#)

  1. In Solution Explorer, select the Class Library project.
  2. From the View menu, choose Property Pages.
  3. In the <Project> Property Pages dialog box, in the Configuration drop-down list box, choose Debug.
  4. In the Configuration Properties folder, choose the Debugging category.
  5. Under Start Action in the properties grid, set the Debug Mode property to Program.
  6. In the Start Application box, click on the ellipsis button to browse for the application.
  7. Enter any necessary program arguments in the Command Line Arguments box,

To invoke a calling application that is an existing program (Visual Basic)

  1. In Solution Explorer, select the Class Library project.
  2. From the View menu, choose Property Pages.
  3. In the <Project> Property Pages dialog box, select the Configuration Properties folder.
  4. In the Configuration drop-down list box, choose Debug.
  5. In the Configuration Properties folder, select the Debugging category.
  6. Under Start Action, choose Start External Program and click the ellipsis button to browse for the application.
  7. Under Start Options, enter any necessary program arguments in the Command line arguments box.

Invoking the Calling Application at a URL

To invoke calling application at a URL (C#)

  1. In Solution Explorer, select the Class Library project.
  2. From the View menu, choose Property Pages.
  3. In the <Project> Property Pages dialog box, select the Configuration Properties folder.
  4. In the Configuration drop-down list box, choose Debug.
  5. In the Configuration Properties folder, select the Debugging category.
  6. Under Start Action in the properties grid, set the Debug Mode property to URL.
  7. In the Start URL box, type the URL, such as file:///C:\MyProjects\NewProject/Caller.exe.

To invoke calling application at a URL (Visual Basic)

  1. In Solution Explorer, select the Class Library project.
  2. From the View menu, choose Property Pages.
  3. In the <Project> Property Pages dialog box, select the Configuration Properties folder.
  4. In the Configuration drop-down list box, choose Debug.
  5. In the Configuration Properties folder, select the Debugging category.
  6. Under Start Action, choose Start URL and click the ellipsis button to browse for the application.

Mixed-Mode Debugging

The calling application that calls your class library can be written in managed code or unmanaged code. If your class library is called by unmanaged code, both managed and unmanaged debuggers must be enabled. You can check this in the <Project> Property Pages dialog box. How you do this depends on whether you start debugging from the Class Library project or the calling application project.

To enable mixed-mode debugging (managed and unmanaged) from a calling application written in C or C++

  1. In Solution Explorer, select the C or C++ project.
  2. From the View menu, choose Property Pages.
  3. In the <Project> Property Pages dialog box, in the Configuration drop-down list box, choose Debug.
  4. In the Configuration Settings folder, choose the Debugging category.
  5. Set Debugger Type to Mixed or Auto.

To enable mixed-mode debugging (managed and unmanaged) from a Class Library (C# or Visual Basic)

  1. In Solution Explorer, select the Class Library project.
  2. From the View menu, choose Property Pages.
  3. In the <Project> Property Pages dialog box, select the Configuration Settings folder.
  4. In the Configuration drop-down list box, choose Debug.
  5. In the Configuration Settings folder, choose the Debugging category.
  6. Under the Debuggers group, select Enable: Unmanaged debugging.

Changing Default Configurations

When you create a Class Library project with the project template, Visual Studio automatically creates required settings for the Debug and Release configurations. If necessary, you can change those settings. For more information, see Debug and Release Configurations.

To change the default Debug configuration

  • Define the DEBUG and TRACE constants, which allow your application to use the Debug and Trace classes.
  • Turn on Generate Debugging Information.
  • In Visual C#, set the Output Path to bin\Debug\. In Visual Basic, set the Output Path to bin\.
  • In Visual C#, set Optimize code to false. (Optimized code is harder to debug, since the generated instructions do not correspond directly to your source code. If you find your program has a bug that appears only in optimized code, you can turn this setting on, but remember that code shown in the Disassembly window is generated from optimized source that may not match what you see in your source windows. Other features, such as stepping, may not behave as expected.)

For details, see Project Settings for a C# Debug Configuration or Project Settings for a Visual Basic Debug Configuration.

See Also

Debugging Managed Code | Debugging Preparation: C# and Visual Basic Project Types

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.