Search code examples
asp.netvisual-studiocassini

Automatically stop/restart ASP.NET Development Server on Build


Is there a way to automatically stop the ASP.NET Development Server (Cassini) whenever I do a build/rebuild in VS2008 (and then obviously have it start again when required)? Maybe there's some hidden configuration setting somewhere? Or at least some way to do it as a post-build event maybe?

For some background, the issue is that I'm using Spring.NET for dependency injection, etc, but it loads its singletons on Application Start, meaning that if I change any spring related code/configuration I have to stop the Development Server, so that it starts again next debug/run ensuring the Application Start event is triggered again. In other words, even if you change a bunch of code/config & then start debugging again, it doesn't actually start again, since its already running, so your new code isn't being used.


Solution

  • So I ended up with a workaround based off Magnus' answer, but using the following relatively simple macro (why do they force you to use VB for macros? I feel all dirty):

    Imports System
    Imports System.Diagnostics
    
    Public Module KillCassini
    
        Sub RestartDebug()
            If (DTE.Debugger.DebuggedProcesses.Count > 0) Then
                DTE.Debugger.Stop(True)
            End If
            KillCassini()
            DTE.Debugger.Go(False)
        End Sub
    
        Sub KillCassini()
            Dim name As String = "WebDev.WebServer"
            Dim proc As Process
            For Each proc In Process.GetProcesses
                If (proc.ProcessName.StartsWith(name)) Then
                    proc.Kill()
                End If
            Next
        End Sub
    
    End Module
    

    Basically if the debugger is currently running, it will stop it & then kill any processes named "WebDev.WebServer" which should be all the Cassini instances and then starts the debugger again (which will implicitly start Cassini again). I'm using proc.Kill() because neither proc.CloseMainWindow() or proc.WaitForExit(1000) seemed to work...

    Anyway, once you've got your macro you can assign it to keyboard shortcuts, or create custom toolbar buttons to run it.