Fabrice's weblog

Tools and Source

News

My .NET Toolbox
An error occured. See the script errors signaled by your web browser.
No tools selected yet
.NET tools by SharpToolbox.com

Read sample chapters or buy LINQ in Action now!
Our LINQ book is also available on AMAZON

.NET jobs

Emplois .NET

transatlantys hot news

Contact

Me

Others

Selected content

Attach to Visual Studio's Development Web Server easily using a macro

Something I have to do frequently is attaching to a running web application for debugging purposes. I don't always start my web applications in debug mode, but I often need to attach at some point when doing some testing.
Unfortunately, there is no easy way to do this from Visual Studio. We have to attach to the server process "by hand". After some time doing this, you'll find it a bit boring...
Here is a quick-n-dirty solution. It's a simple macro that attaches to the first web server process it finds. Just copy the code below to your macros using the Macro IDE (Tools | Macros | Macro IDE...).
Once you have the macro, you can:
  • add a button to a toolbar by right-clicking on the toolbars, then "Customize... | Commands | Macros" and drag & drop the command on the toolbar of your choice
  • map a shortcut key to it using "Tools | Options | Environment | Keyboard" and search for the AttachToFirstDevWebServer
Warning 1: this code attaches to the first server it finds, not necessarily the right one. Unfortunately, there is no way to find the correct web server as the processes do not provide information such as a port number or other useful information.
Warning 2: the code searches for Visual Studio 2005's integrated Development Web Server. To attach to IIS instead (from VS 2003 for example), you'll have to adapt the code to attach to aspnet_wp.exe.

' This subroutine attaches to the first Development Web Server found.
Public Sub AttachToFirstDevWebServer()
  Dim process As EnvDTE.Process

  For Each process In DTE.Debugger.LocalProcesses
    If (Path.GetFileName(process.Name).ToLower() = "webdev.webserver.exe") Then
      process.Attach()
      Exit Sub
    End If
  Next

  MsgBox("No ASP.NET Development Server found")
End Sub

Update: you may have to replace "webdev.webserver.exe" by "webdev.webserver2.exe"

Comments

Dario said:

muy bueno, excelente. util para depurar un smartclient (winForm y webservice)
# May 8, 2006 5:13 PM

fx said:

Fantastic! Thanks ...
# June 16, 2006 11:01 AM

Stephen said:

much thanks for the 1 click attach to process

# August 22, 2006 12:58 PM

Dready said:

Great, I got tired of doing it myself within 10 times.

# April 6, 2007 2:28 PM

Dean W. Poulin said:

FYI:  to get this to show up as a command you can add, make sure that you add the method to a public Module and change the signature above to be Public Sub.

IE:

Imports System

Imports EnvDTE

Imports EnvDTE80

Imports EnvDTE90

Imports System.Diagnostics

Imports System.IO

Public Module VSMacros

   Public Sub AttachToFirstDevWebServer()

       Dim process As EnvDTE.Process

       For Each process In DTE.Debugger.LocalProcesses

           If (Path.GetFileName(process.Name).ToLower() = "webdev.webserver.exe") Then

               process.Attach()

               Exit Sub

           End If

       Next

       MsgBox("No ASP.NET Development Server found")

   End Sub

End Module

# October 9, 2007 11:47 PM

Sidar Ok said:

Thanks for it this is great. I have written something similar to this macro too but I was using it for locally deployed applications so the name of the application was aspnet_wp.exe for me for who need the same.

# May 31, 2008 7:27 AM

karpach96 said:

Finally I got tired of attaching it manually. I am glad that I found this post, before I wrote my own macros. Thank you, Fabrice.

# November 17, 2008 3:08 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)