views:

2902

answers:

3

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.

+3  A: 

The only way that I know of is to do a custom Cassini startup on the post.build event. This custom made process kills all instances of Cassini, and startup a new one. In order to get this to work, you will need to build a small custom command line utility. I have called it SpawnProcess here.

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Diagnostics;

namespace SpawnProc
{
  class Program
  {
    public static void Main(string[] args)
    {
      if (args.Length > 0)
      {
        // Kill all current instances
        FileInfo fi = new FileInfo(args[0]);
        string name = Path.GetFileNameWithoutExtension(fi.FullName);
        foreach (Process proc in Process.GetProcessesByName(name))
        {
          proc.Kill();
        }

        ProcessStartInfo startInfo = new ProcessStartInfo(args[0]);
        if (args.Length > 1)
        {
          startInfo.Arguments += "/port:" + args[1];
        }

        if (args.Length > 2)
        {
          startInfo.Arguments += " /path:\"" + args[2].Trim(new char[]{'"'}) + "\"";
        }
        if (args.Length > 3)
        {
          startInfo.Arguments += " /vpath:\"" + args[3].Trim(new char[]{'"'}) + "\"";
        }

        try
        {
          Process.Start(startInfo);
        }
        catch (Exception ex)
        {
          Debug.WriteLine("Error: " + ex.Message);
          for (int i = 0; i < args.Length; i++)
          {
            Debug.WriteLine("args[" + i + "]: " + args[i].ToString());
          }
        }
      }
    }
  }
}

Then you will have instruct Visual Studio to not use Cassini. Got to properties for your web application -> Web and select "Use Custom Web Server", enter something like:"http://localhost:1685/". (Or whatever port number you would like to use). Then, enter this command in the post-build event:

"$(ProjectDir)..\SpawnProc\bin\debug\SpawnProc" "C:\Program Files (x86)\Common Files\microsoft shared\DevServer\9.0\WebDev.WebServer.exe" 1685 "$(ProjectDir)" /

Make sure your paths is correct, for instance, since I'm running a 64bit OS, my Program files path is different from a 32bit OS. Also, my SpawnProc.exe is in a sub project.

Hope this helps.

/Magnus

Magnus Johansson
Thanks Magnus, although I didn't use your exact solution, I used the idea to create a macro, which for my purposes is a bit simpler.
Alconja
+3  A: 

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 = 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.

Alconja
+4  A: 

http://blogs.msdn.com/webdevtools/archive/2007/12/13/workaround-debugging-global-aspx-cs-application-start-with-asp-net-web-server-within-visual-studio.aspx

Enabling "Edit & Continue" on the web server project worked for me. It doesnt shutdown cassini when you stop debugging, but it does restart cassini when you start debugging.

mat3
Alconja
on visual studio 2008 it also closes the web server on stopping debug
Jaguar