32

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.

Alconja
  • 14,662
  • 3
  • 58
  • 61
  • I would be curious why you need that without that background you provided. :) Can't help anyway. – Arnis Lapsa May 20 '09 at 08:06
  • 1
    I have the same desire but a very different problem. My web app has native dependencies (database drivers), and our build copies them into the output directory. (The .NET wrappers around the drivers have static bindings to the native DLLs, allowing us to place them in the `bin` directory. Trust me. It's a much better solution than trying to install Oracle client.) Unfortunately, the development server holds locks on the native DLLs, so copying them fails during the build. It's very frustrating having to manually stop the web server every time I need to rebuild. – jpmc26 Apr 08 '14 at 14:46

6 Answers6

23

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.

Albireo
  • 10,294
  • 12
  • 57
  • 95
mat3
  • 779
  • 6
  • 12
  • Nice (& simple) solution. I'll keep using my macro now its already written & gives me finer grained control, but this is still good to know. Thanks. – Alconja Jan 20 '10 at 02:54
  • on visual studio 2008 it also closes the web server on stopping debug – Jaguar Jul 15 '10 at 08:50
  • On Visual Studio 2010 it does as well. (It doesn't clear the notification area icon until you hover over the icon.) – Ryan Lundy Feb 25 '11 at 14:54
  • Somehow my VS2010 is acting exactly how Kyralessa describes BUT i never have played with this "Edit & Continue" setting and it seems to do nothing when i do change it :( I dont want to reset every time i stop :( – felickz Aug 26 '11 at 01:30
9

I just open a command line (runas admin)

run the following. It should kill all of them

Taskkill /IM WebDev.WebServer40.EXE /F
Jesse
  • 8,035
  • 7
  • 42
  • 56
wlopez
  • 91
  • 1
  • 1
9

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.

Community
  • 1
  • 1
Alconja
  • 14,662
  • 3
  • 58
  • 61
  • Note: modified by an anonymous editor to change `proc.ProcessName = name` to `proc.ProcessName.StartsWith(name)` so that it continues to work with VS2010. – Alconja Jul 05 '12 at 23:08
7

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.

Magnus Johansson
  • 26,737
  • 17
  • 96
  • 157
  • 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 May 21 '09 at 04:06
  • @Kiquenet , can you please elaborate on your edit? Why do you think there is a need for a FQDN for a local dev environment with Cassini that only works for localhost anyway? – Magnus Johansson Oct 10 '14 at 20:15
  • @Sorry, you completely lost me. I am changing it back to Localhost. – Magnus Johansson Oct 13 '14 at 12:05
5

Inspired by this post and another one about code clean up I added the macro as PostDebug-event. So every time the debugger returns, it will remove all WebDev.WebServer-s. (And I relaxed the ProcessName-constraint.)

Note: this will probably kill all the WebServers, so also WebServers of other debug-sessions (which is fine with me, at this moment, I usually don't have any). So you may want to only look for child-processes or something like that (and post that code here ;-) ).

So my code looks like this:

Private Sub DebuggerEvents_OnEnterDesignMode(ByVal Reason As EnvDTE.dbgEventReason) _
            Handles DebuggerEvents.OnEnterDesignMode
    If (Reason = dbgEventReason.dbgEventReasonStopDebugging) Then
        Dim name As String = "WebDev.WebServer"
        Dim proc As System.Diagnostics.Process
        For Each proc In System.Diagnostics.Process.GetProcesses()
            If (proc.ProcessName.StartsWith(name)) Then
                proc.Kill()
            End If
        Next
    End If
End Sub
Community
  • 1
  • 1
Yahoo Serious
  • 3,080
  • 1
  • 29
  • 34
0

Another way is using Powershell:

PS: I don't know if anybody is still in need, but I just accidentally ran into this solution, while looking for something completely different.

Yahoo Serious
  • 3,080
  • 1
  • 29
  • 34