Tip

Debugging hosted .NET applications


.NET Developer Tip
(Receive this column in your inbox,
click Edit your Profile to subscribe.)

Debugging hosted .NET applications

If you're going to build enterprise

    Requires Free Membership to View

solutions, some of your assemblies are bound to be hosted by other applications. A huge part of knowing how to code is having good debugging skills, but hosted assemblies can be challenging to debug unless you know that Visual Studio .NET supports debugging hosted applications by attaching to the host directly. This tip from InformIT, explains how to debug hosted applications.

Attaching to the Host Application

Our focus is on testing the serviced component rather than the client application. To accomplish the testing with the client and service in the same assembly, we can set the client as the startup project but start without debugging by selecting Debug, Start Without Debugging. This step will start the client, leaving the debugger available to debug the hosted application.

After the client is running, click the button in the application to create and load an instance of the host process, dllhost.exe. When the host process is running, we can use the available integrated debugger—because we started the client without debugging—and attach the debugger to the host process. To attach to the host process, follow these steps (using the associated figures as a visual guide):

  1. Select Debug, Processes to display the Processes dialog box.
  2. Navigate to the dllhost.exe process, click it, and click the Attach button. In the list called Choose Program Types That You Want To Debug, check only the Common Language Runtime option, and click OK.
  3. Click the Close button in the Process dialog box. The dllhost.exe host will be shown in the Debugged Processes list in the Processes dialog box.

You may run into a bit of trouble if several instances of your host are running. For example, if an instance of dllhost.exe is running on behalf of some other, non-managed service, you'll get an error indicating that the host needs to be running managed code to debug with the .NET debugger. Additionally, if you select more than the Common Language Runtime option —for example, if you select Native—the debugger takes a lot longer to load.

After you've attached to the host process and have clicked Close, the integrated environment will load the required assemblies and modules needed to debug your hosted application. In our example, these will be the binaries needed to support the LuckyNumberGenerator.

Debugging and Testing Your Hosted Assembly

After the debugger has attached to your assembly's host application, you can debug in .NET as you would debug anything else. You can set breakpoints and watches; examine the stack or memory; view and switch between threads, if applicable; and use the Immediate/Command window to run and explore code while in break mode.

To try out debugging a hosted assembly, set a breakpoint in the GetLuckyNumber method in the service. Switch focus to the client and click the button we placed in the form. If everything is pieced together correctly, the IDE will break at the point you indicated.

At this point, you can perform any of the operations you might normally perform when debugging an executable or DLL that's not hosted by another process.

Finished Debugging?

When you're finished debugging, you can select Debug, Stop Debugging or Debug, Detach All. Selecting Stop Debugging may kill the host process. If you want to continue running the host application, attach to another host, or attach another service to the same host, select Detach All, which will detach the debugger from the host, leaving the host intact.

TIP: You also have the option of detaching or terminating the host from the Processes dialog box.

You can prevent the IDE from killing the host process by changing the When Debugging Is Stopped option. If you want the host to terminate when debugging stops, select Terminate This Process. If you want the host to remain intact, select Detach from This Process.


Read more about creating COM+ serviced components and to see figures of the process in the complete article at InformIT.


This was first published in May 2004

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.