Skip to content

Attach process at runtime in visual studio 2008

I have a wcf service which has to be unittested. The wcf service is hosted in a console application. In the unittest, i want the possibility to debug this wcf service. For this, the process has to be attached to the visual studio(2008 in my case) debugger.

You just have to add a reference to EnvDTE, and to add the following pice of code. With that, visual studio attaches the given process to the current debug session.

  2.     EnvDTE.DTE dte2 = (EnvDTE.DTE)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.9.0");
  3.     EnvDTE.Debugger debugger = dte2.Debugger;
  4.     foreach (Process program in debugger.LocalProcesses)
  5.     {
  6.         if (program.Name.Contains("MyApp.exe"))
  7.         {
  8.                         program.Attach();
  9.         }
  10.     }

It's handy, its easy and there is not a lot of documentation about it... There are a lot of other EnvDTE(80/90) assemblys, and i guess they are for specific visual studio versions. But i don't see the benefit in using them.