Attaching to a Process using VS.NET Automation Model
I've recently changed NUnitAddin so that it runs unit tests in an external process. It controls and communicates with this process using .NET remoting. Because tests are run in an external process it's possible to attach the VS.NET debugger set breakpoints on them. When I manually attach the CLR debugger (as follows) it works great.
I've added a 'Debug Tests' item to the Tools menu with the intention of automatically attaching the debugger to the correct process. It is strait forward enough to query the remote test runner for its process id. After doing this I’ve tried attaching the VS.NET debugger using the following code…
public void AttachToProcess(int processId){
foreach(EnvDTE.Process process in DTE.Debugger.LocalProcesses){
if(process.ProcessID == processId){
process.Attach();
DTE.Debugger.CurrentProcess = process;
}
}
}
The debugger appears to attach to the process, but when I try running a test it just stops. I get the same effect if I manually select CLR and Native debugging at the same time. I suspect this is what’s happening when calling the above code.
At this point I'm pretty stumped. I've done a workaround that involves launching the test runner process using a project with debug type 'Program'. This kind of works, but is far from ideal.
What I really need is a way of programmatically attaching a specific instance of VS.NET to a process using CLR debugging. Any help would be greatly appreciated. Can anyone shed light on why selecting CLR and Native debugging at the same would cause an application to freeze?
I've made a zip of the addin that uses the above code here. Do a search for 'AttachToProcess' in the 'NUnitAddin\Connect\NUnitAddinConnect.cs' file. The main solution target will compile, install and launch a test solution using the addin (it couldn't be easier). Once you've done that you're ready to 'Debug Tests' on the 'Tools' menu and 'Run Test(s)'...