Rajeeshcv.com

Sharing my knowledge

Attach to Any ASP.NET Web Server from Visual Studio in One Click

Posted on: 30 Nov 2011 | Filed under: VisualStudio, ASP.NET, .NET | 3 comments

This is an update to my previous blog post Attach to Visual Studio Development Server with One Click.

The Visual Studio Macro from previous article doesn’t support IISExpress or IIS; it only supported the Visual Studio Development Server, more over it doesn’t detect latest Development Web Server “WebDev.WebServer40.exe”.

Now I have updated the Macro so that it will automatically detect the Web Server setting from the project properties and attach it accordingly.

Below is the code for the new Macro, I think it is self-explanatory

Public Sub AttachToWebServer()
    Dim attached As Boolean = False
    Dim project As EnvDTE.Project = GetStartupProject()

    If (project Is Nothing) Then
        MsgBox("Couldn't find a web project that can be attached")
        Return
    End If

    attached = AttachToProcess(project)

    If (Not attached) Then
        MsgBox("Couldn't attach to the process")
    End If
End Sub

Private Function GetStartupProject() As EnvDTE.Project
    Dim startUpProject As String = DTE.Solution.Properties.Item("StartupProject").Value

    For Each currentProject As EnvDTE.Project In DTE.Solution.Projects
        If currentProject.Name = startUpProject Then
            Return currentProject
        End If
    Next
    Return Nothing
End Function

Private Function AttachToProcess(ByVal project As EnvDTE.Project) As Boolean
    Dim serverProcessNamePattern As String
    ' Using either IIS express or IIS
    If project.Properties.Item("WebApplication.UseIIS").Value = "True" Then
        ' Using IIS Express
        If project.Properties.Item("WebApplication.DevelopmentServerCommandLine").Value.ToString().Length > 0 Then
            serverProcessNamePattern = ".*iisexpress.exe"
        Else ' Real IIS
            serverProcessNamePattern = ".*w3wp.exe"
        End If

    Else ' Assume development web server
        serverProcessNamePattern = ".*WebDev.WebServer\d+.EXE"
    End If

    Return AttachToWebServer(serverProcessNamePattern)

End Function

Private Function AttachToWebServer(ByVal serverProcessNamePattern As String) As Boolean

    Dim attached As Boolean = False

    For Each process In DTE.Debugger.LocalProcesses
        If (Regex.IsMatch(process.Name, serverProcessNamePattern)) Then
            process.Attach()
            attached = True
            Exit For
        End If
    Next

    Return attached
End Function

Read my previous article Attach to Visual Studio Development Server with One Click to understand how we can add this Macro as command to the Visual Studio toolbar.

If you like this please share :

Comments

TweeZz

TweeZz

Commented 2 months ago

Thanks for this one! :)


Jason

Jason

Commented one month ago

Thank you. Real developers use IIS plain and simple, and there is no argument as to why you'd use some other crap. That is what we should be using, not some pile Cassini or IIS Express. What the heck is this IIS Express crap. Just install IIS on your local machine, big deal. Doesn't slow mine down. Why not have the full gamut of options available in IIS so you can learn or if you need them at times, you never know. I don't want to have to go back and install more items for IIS, I just install most all of it pretty much with a few exceptions.

So it's good you created this cause true developers would like to use this nice shortcut that runs over a real web server. Not some black box Cassini crap.


Rajeesh

Rajeesh

Commented one month ago

@Jason Thanks for your comment.

I agree with you on the simplicity of setting IIS as the webserver but we can't ignore the real purpose of Cassini or IIS Express.

Some of the developers won't have the luxury of installing IIS on their development machine due to the company policies, even some of them won't be having full administrator right on their machines also. That is the space were these small development servers fits in.

Actually I prefer IIS express or Cassini for my day to day development work.


Leave your comments