Automatically stop/restart ASP.NET Development Server on Build

两盒软妹~` 提交于 2019-11-27 18:31:23
mat3

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.

Alconja

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.

wlopez

I just open a command line (runas admin)

run the following. It should kill all of them

Taskkill /IM WebDev.WebServer40.EXE /F

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.

Yahoo Serious

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!