6

我通常在开发网络应用程序,并且我的工作时间花费了大量的时间来完成“Ctrl + Alt + P”,按进程名排序,然后选择w3wp.exe来附加我的调试器。如何使Visual Studio宏将调试器附加到w3wp.exe的所有实例?

更糟糕的是,我正在开发跨越多个应用程序池的应用程序,所以我通常会有2或3个w3wp.exe实例,并且不可能知道要附加哪一个实例,所以我通常会结束附加到所有这些,这是过度杀伤力,但工程。

总而言之,这是烦了...

我的同事想出了一个办法有一个VS宏自动附着到W3WP.EXE(他基本上记录了这个):

Sub AttachMacro()  
    Try  
    Dim dbg2 As EnvDTE80.Debugger2 = DTE.Debugger  
    Dim trans As EnvDTE80.Transport = dbg2.Transports.Item("Default")  
    Dim dbgeng(3) As EnvDTE80.Engine  
    dbgeng(0) = trans.Engines.Item("T-SQL")  
    dbgeng(1) = trans.Engines.Item("T-SQL")  
    dbgeng(2) = trans.Engines.Item("Managed")  
    Dim proc2 As EnvDTE80.Process2 = dbg2.GetProcesses(trans, "ENIAC").Item("w3wp.exe")  
    proc2.Attach2(dbgeng)  
    Catch ex As System.Exception  
    MsgBox(ex.Message)  
    End Try  
End Sub 

我真的不确定是否所有这些都是必要的,或者说,我从来没有为VS做过宏,我真的不知道从哪里开始。

会不会有改变这个宏的方式,这样,而不是其自身附加到的w3wp.exe的例如,它会将自身附加到的w3wp.exe的所有实例?

+0

此功能真的应该是Visual Studio的一部分。任何人想要做出连接问题 - 或检查现有的问题? – 2013-02-19 07:06:28

回答

8
Sub MacroAttachToAllProcesses() 

    Try 

     Dim dbg2 As EnvDTE80.Debugger2 = DTE.Debugger 
     Dim trans As EnvDTE80.Transport = dbg2.Transports.Item("Default") 
     Dim dbgeng(3) As EnvDTE80.Engine 

     dbgeng(0) = trans.Engines.Item("T-SQL") 
     dbgeng(1) = trans.Engines.Item("T-SQL") 
     dbgeng(2) = trans.Engines.Item("Managed") 

     For Each theProcess As EnvDTE80.Process2 In dbg2.GetProcesses(trans, "COMPUTERNAME") 
      If theProcess.Name.Contains("w3wp.exe") Then 
       theProcess.Attach2(dbgeng) 
      End If 

     Next 

    Catch ex As System.Exception 
     MsgBox(ex.Message) 
    End Try 

End Sub 
+0

进一步,并将其分配给工具栏...滚动到底部这篇文章:http://www.milkcarton.com/blog/2007/05/20/Using+Visual+Studio+Macros+To+Increase+Productivity.aspx – 2009-12-04 17:12:01

0

你可能想看看gflags.exe。它的一个选项是一个调试器,以运行附加到每个特定可执行文件的调用。

+0

我不确定这会工作... 这是怎么回事?每次运行w3wp.exe时都打开一个Visual Studio的新实例? 我需要几个w3wp.exe进程连接到相同的VS实例,我需要它是我的项目加载的实例... – 2009-05-05 17:48:07

1

我知道您正在为此任务寻找宏,并且我拥有类似的宏。不过,我想解释一下在开始调试时将调试器附加到解决方案中的项目的方法。

这是一个鲜为人知的功能 - 如果您在解决方案浏览器中右键单击解决方案文件,选择属性,然后可以定义多个启动项目及其操作。运行时,您的调试器将附加到列出的项目。

注意:如果您有网络服务,它将打开一个浏览器窗口,但是您可以通过禁止它打开窗口来禁用该项目的属性。

1

这是我如何附加到远程w3wp进程。它运行速度比DanC的解决方案快一些,并且有一些额外的错误处理。

Private Sub AttachToW3wp(ByVal machineName As String) 
    ' In order for this to work, you have to be running the Visual Studio 2010 Remote Debugging Monitor 
    ' as your (domain) user. 
    ' It won't work if the remote debugger is running as a service. I've tried every permutation of 
    ' domain and username in the the transport qualifier, tried the obvious local system username, 
    ' even tried looking at the network traffic 
    ' in WireShark, I can't figure it out how to make it work if you are running as a service. 
    ' If you are running the debugger as a service, even running the macro that gets created by VS's 
    ' macro recorder when you attach to a process doesn't work. 
    Dim transportQualifier = machineName 
    Try 
     Dim processToAttachTo As String = "w3wp.exe" 
     Dim dbg2 As EnvDTE80.Debugger2 = DTE.Debugger 
     Dim trans As EnvDTE80.Transport = dbg2.Transports.Item("Default") 
     Dim dbgeng(2) As EnvDTE80.Engine 
     dbgeng(0) = trans.Engines.Item("T-SQL") 
     dbgeng(1) = trans.Engines.Item("Managed (v2.0, v1.1, v1.0)") 
     Dim processesRemote = dbg2.GetProcesses(trans, transportQualifier) 
     Dim attached As Boolean = False 
     Dim processRemote As EnvDTE80.Process2 
     For Each processRemote In processesRemote 
      ' For some reason it takes a much longer time to get the remote process names then it 
      ' does the user name, so let's skip over all the processes that have the wrong UserName. 
      If processRemote.UserName = "NETWORK SERVICE" AndAlso _ 
       (Right(processRemote.Name, Len(processToAttachTo)) = processToAttachTo) Then 
       If processRemote.IsBeingDebugged Then 
        MsgBox(processToAttachTo & " on " & machineName & " is already being debugged") 
       Else 
        processRemote.Attach2(dbgeng) 
       End If 
       attached = True 
      End If 
     Next 
     If Not attached Then 
      MsgBox(processToAttachTo & " is not running on " & machineName & ".") 
     End If 
    Catch ex As System.Exception 
     MsgBox("Exception attempting to attach to " & transportQualifier & ": " & ex.Message) 
    End Try 
End Sub 
相关问题