Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 956
Alconja Avatar asked May 20 '09 07:05

Alconja


4 Answers

Workaround: Debugging Global.aspx.cs Application_Start() with ASP.Net Web Server within Visual Studio

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.

like image 192
mat3 Avatar answered Nov 13 '22 02:11

mat3


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.

like image 26
Alconja Avatar answered Nov 13 '22 00:11

Alconja


I just open a command line (runas admin)

run the following. It should kill all of them

Taskkill /IM WebDev.WebServer40.EXE /F
like image 9
wlopez Avatar answered Nov 13 '22 00:11

wlopez


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.

like image 7
Magnus Johansson Avatar answered Nov 13 '22 00:11

Magnus Johansson