Attaching to select processes in Visual Studio - arcdev/engram404 GitHub Wiki
originally posted 2016-10-07 at https://engram404.net/attaching-to-select-processes-in-visual-studio/
Many of us who write software daily find that we need to attach Visual Studio to already running processes. This is built-in to VS and fairly simple – as long as you only have one instance of a given process.
(If you just want the VS extension go to AnotherAttachToAny, or feel free to skip to The current solution, or keep reading)
- IIS hosting a web site (call this UI)
- IIS hosting a services site (call this Services)
- IIS hosting an admin web site (call this AdminUI)
- IIS hosting an admin services site (call this AdminServices)
This is likely to be multiple IIS AppPools and therefore multiple IIS worker processes (w3wp.exe).
In this case, your choices are:
- attach to all of them
- guess which one is the right one – good luck
- assign different users to different processes
Attaching to all of the processes isn't necessarily terrible, but it tends to be slow – especially if you've got a large application. It's also challenging if you're trying to debug unhandled exceptions which can get thrown in any of the processes. And it can get rather time-consuming if you have to attach-debug-change-compile/build-repeat.
Guessing which one is the right one – this is pretty obviously difficult.
Assigning different users definitely makes life easy, but it means configuring all of the different user accounts to have the '"right'" permissions. (Many times in production-like environments these sites may be spread across different servers and will each execute as the same service account.)
You can see that, while VS can tell you the process owner, you'll still have to navigate to the process, then select the correct one by owner.
(side note: There's a bit of fun with Visual Studio attaching to a process owned by another user. Check out How To Disable the Attach Security Warning dialog in Visual Studio.)
In Visual Studio 2012, I wrote a simple little add-in that would inject new menu items into the Debug menu with some preconfigured process-and-owner combinations. (The configuration was stored in an XML file – I was lazy.)
This worked phenomenally well…until the Visual Studio team removed the support add-ins entirely. (They were technically deprecated in VS2013, but were completely removed in VS2015, if I recall correctly. [link])
My first thought was simply to convert my add-in to a VSPackage, but that was more difficult than Microsoft makes it appear (due to the differences in the framework).
The next thought was to look for any existing extension.
That lead me to Ryan Conrad's[](https://visualstudiogallery.msdn.microsoft.com/81677d17-6e81-4f14-87cc-4ccee2fd2589>AttachToAny.
Ryan's extension was an awesome start and really helped me get going.
Ryan was kind enough to provide his extension as open source on Github.
His original extension was relatively basic (that's not a dig – it did everything it was designed to do).
For me, though, it lacked the ability to connect to processes by owner/username so that was the first feature I added. Then, I got hooked.
I found that he had a feature in place already that would prompt you if there were multiple matching processes – to select which one(s) you wanted. This was a great step in the right direction and certainly saved a few clicks (compared with the native Visual Studio dialog). But then I found a glittering gem! He had added a bit of code that, when working with IIS Worker Processes (w3wp.exe), presented you with the AppPool name.
I took that a bit farther than added support for it right into the tool itself.
So now you can attach based on process name, process owner/username, and/or IIS AppPool name. (I extended it one bit farther by allowing you to tell the extension that any given value is a regex – giving you even more flexibility.)
The final notable addition from me was the addition of shortcut keys in the menu. I'm a HUGE proponent of making things keyboard-accessible. (I hate picking up the mouse – it just slows things down.)
Check it out in the Visual Studio Gallery here.
Or you can search for it within Visual Studio's Extensions and Updates dialog:
Here are some screenshots of the tool (mostly credit to Ryan Conrad).
I have a couple other things in mind for future features:
- add a more '"global'" setting to treat all of the properties as regular expressions
- add multi-process handling; as in when there are multiple matches attached to: all, none, prompt, first, last, random
- convert the automatically generated keyboard shortcuts to a user-configurable property
- consider some global, default keyboard shortcuts (probably chords)