Design-Time Debugging ASP.NET 2.0 in Visual Studio 2005

August 30th, 2006

If you have been doing control development with ASP.NET 2.0 you are likely working with with the CompositeControl or the CompositeDataboundControl. And as you taught yourself to use these base classes you read a great deal from great authors like Miguel Castro and Dino Esposito.

I have personally soaked up as much as I can from these guys so that I can build powerful controls with helpful design-time support. And it has been easy to add advanced features like control designers, smart tags and editable templates. But there comes a point when what should be working, suddenly stops working. And since you have all this code supporting the design-time you cannot simply hit F5 to enter the debug mode. As yet the MSDN documentation on design-time debugging still only covers Visual Studio 2003. Some of the steps still match what you need to do with Visual Studio 2005, but it is different enough to make it difficult to figure it out. Today I finally figured it out and put together a step-by-step tutorial on how it is done.

We will start out with these assumptions.

  • The Debug configuration is active
  • A class library holding a control exists in the Solution
  • A website referencing that class library exists in the solution

Your view of the Solution Explorer should look fundamentally like the picture to the right. To set up debugging we will adjust the properties of the class library for the Debug configuration. Go ahead and right-click the project in the Solution Explorer and select Properties to load the Properties window for the class library.

On the Properties screen you will see multiple options along the left side. You want to select Debug and set it to "Start external program." Then click the ellipsis button to the right of the text box to browse to devenv.exe, the Visual Studio 2005 executable. For my installation it is located under the C:\Program Files folder.


C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\devenv.exe

Click the Save button and close the Properties screen. While you still have the class library selected in the Solution Explorer click Debug on the menu. It does not reflect the context for the Properties you just changed. And simply starting the debug process at this point will give you some unpredictable results.

Pay attention to the Startup Project. For my web projects I have the Website set as the Startup Project. To change it you simply right click the project in the Solution Explorer and click "Set as Startup Project" which tells the debugger to start there. Often times Visual Studio is smart enough to open the currently selected web form in a web browser when you hit F5 to start the debug process. You will normally be doing that, so leave the Website as the Startup Project.

Now you are ready to launch the debug process. You will be starting another copy of Visual Studio, so you will be using much more memory than just a single running copy. It may be a good idea at this point to close unnecessary applications which use a lot of the system resources which will be used heavily while debugging. SQL Server Management Studio is one I always shut down when I need to conserve resources.

Right click on the class library project and move the mouse down to the Debug option. You will see it gives you a couple of options. Click on Start new instance and a fresh copy of Visual Studio will start.

As with any debugging you will want to set your breakpoints by clicking the gutter to the left of the code. You may or may not have line numbers turned on. You can set your breakpoints while you wait for the new instance of Visual Studio to start. It may take several moments for it to be fully started.


The new Visual Studio instance will not have a Solution or Project loaded, but so you will want to created one. Select File from the menu and create a new website. This website will hold a web form which will act as your debugging container for the control. Set up this website with whatever basic needs your website has for design-time support. You could even open an existing website so long as it not the same website that is referenced in the first copy of Visual Studio. Pointing to the same website instance from 2 running copies of Visual Studio in this situation will typically crash both copies of Visual Studio.

With your basic website in place, add a reference to the assembly holding your control. You do not want to add a project reference at this point because the class library project is being used by the first instance of Visual Studio. You want it to reference the most current copy of the debug assembly from your class library, so browse to that assembly and select it. When you add this assembly reference, notice it creates a file in the Bin folder with a .refresh extension added to the name of the assembly. This tells Visual Studio to keep your local copy current with the source location so you do not need to keep adding back updates for this reference.


Once you have the control assembly set up for this website you can update your toolbox to include the control you want to debug. Open the toolbox and expand the General section or create your own section. Right click in it and click on Choose Items. Again, browse to the debug assembly holding the controls. Once you have it selected the controls in that assembly should automatically be selected. Click OK to close the dialog.


You will now see your control listed in the toolbox. The next step will likely trigger the design-time support of your control and cause it to stop at one of your breakpoints. When that happens it will bring forward the first instance of Visual Studio and show you the current line where the breakpoint caused the runtime to pause. And if you have a runtime error before your breakpoint you may get a nasty error message. Either way, that is what should expect from the debugging process.


Open your empty web form and select the Design mode using the tab at the bottom. Then click and drag the control from the toolbox onto the web form and watch as Visual Studio fires off the debugging process.

You can now click away at F10 (Step Over), F11 (Step Into) and F5 (Continue) to walk through your code, including the code which only executes only during design-time. To end your debug session, return to the first instance of Visual Studio and end debugging from the Debug file menu. Doing so will close the second copy of Visual Studio.

When you want to return to debug mode, you can skip the step of creating the empty website. You can simply select the one you already created. Since you will be going through multiple iterations to debug and test your design-time support you will find this saves you time.

Good luck!

Additional Resources


[ Web Development Tools for the Power Developer ]

9 Responses to “Design-Time Debugging ASP.NET 2.0 in Visual Studio 2005”

  1. Brennan’s Blog » Blog Archive » Debugging ASP.NET 2.0 with Visual Studio 2005 Says:

    [...] Design-Time Debugging ASP.NET 2.0 in Visual Studio 2005 [...]

  2. Sony Says:

    Nice article. You should check out the book Developing Microsoft ASP.NET Server Controls and Components / Nikhil Kothari, Vandana K. Datye. There's a small section in a chapter in it about this stuff; "Debugging Design-Time Code"

    Greetz from Sweden

  3. Nick R Says:

    You were hit the mark of my problem.
    Thanks a lot from Russia

  4. Konrad Says:

    Thanks for the article, I was really stuck trying to debug a custom control.
    I found that after I added the control's project to my solution (I had to do a File->Open project with the add to solution radio button selected because I didn't have the add class library option in the context menu when I right-clicked the solution) I was able to debug straight from the same web project, without starting a separate instance of VS2005.

  5. Mike Says:

    Thanks! Good article, very clear.

    Design time sure is tough!

  6. Ross Presser Says:

    Thank you so much for this clear explanation of the required steps. It helped me pinpoint the problem with a localization control library I got from codeproject.com, and it has sped up my development at least fourfold.

  7. henk de bakker Says:

    Thanks i finaly made it width these steps to debug my design-time code.
    Much clearer than al the ms tech ... stupid... help

    Thanks

  8. Arsus Says:

    10x!

  9. Mike Says:

    Wow, I had no idea you could do this. You just opened up whole new doors for me. Thanks.