在Build上自动停止/重新启动ASP.NET Development Server

Alc*_*nja 32 asp.net cassini visual-studio

有没有办法在VS2008中进行构建/重建时自动停止ASP.NET开发服务器(Cassini)(然后显然在需要时再次启动)?也许在某处有一些隐藏的配置设置?或者至少某种方式可以作为一个后期构建活动吗?

对于某些背景,问题是我使用Spring.NET进行依赖注入等,但它在Application Start上加载它的单例,这意味着如果我更改任何与Spring相关的代码/配置,我必须停止开发服务器,所以它再次启动下一次调试/运行,确保再次触发Application Start事件.换句话说,即使您更改了一堆代码/配置然后再次开始调试,它实际上也不会再次启动,因为它已经在运行,因此您的新代码未被使用.

mat*_*at3 23

解决方法:使用Visual Studio中的ASP.Net Web Server调试Global.aspx.cs Application_Start()

在Web服务器项目上启用"编辑并继续"对我有用.当你停止调试时它不会关闭cassini,但是当你开始调试时它会重启cassini.


Alc*_*nja 9

所以我最终得到了一个基于Magnus回答的解决方法,但是使用下面相对简单的宏(为什么它们会强制你使用VB进行宏?我感觉很脏):

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
Run Code Online (Sandbox Code Playgroud)

基本上,如果调试器当前正在运行,它将停止它然后终止任何名为"WebDev.WebServer"的进程,该进程应该是所有Cassini实例,然后再次启动调试器(这将隐式启动Cassini).我正在使用,proc.Kill()因为既没有proc.CloseMainWindow()proc.WaitForExit(1000)似乎工作......

无论如何,一旦你有了你的宏,你可以将它分配给键盘快捷键,或创建自定义工具栏按钮来运行它.


小智 9

我只是打开一个命令行(runas admin)

运行以下.它应该杀死所有人

Taskkill /IM WebDev.WebServer40.EXE /F
Run Code Online (Sandbox Code Playgroud)


Mag*_*son 7

我所知道的唯一方法是在post.build事件上执行自定义Cassini启动.此自定义过程会杀死所有Cassini实例,并启动一个新实例.为了使其工作,您需要构建一个小型自定义命令行实用程序.我在这里称它为SpawnProcess.

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());
          }
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

然后,您将指示Visual Studio不使用Cassini.获取Web应用程序的属性 - > Web并选择"使用自定义Web服务器",输入以下内容http://localhost:1685/:(或者您要使用的任何端口号).然后,在post-build事件中输入以下命令:

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

例如,确保您的路径正确,因为我运行的是64位操作系统,我的程序文件路径与32位操作系统不同.另外,我的SpawnProc.exe位于子项目中.


Yah*_*ous 5

受这篇文章和另一个关于代码清理的启发, 我将宏添加为PostDebug-event.因此,每次调试器返回时,它都将删除所有WebDev.WebServer-s.(我放宽了ProcessName约束.)

注意:这可能会杀死所有的Web服务器,所以其他调试会话的Web服务器也是如此(这对我很好,此时此刻,我通常没有).所以你可能只想查找子进程或类似的东西(并在这里发布代码;-)).

所以我的代码看起来像这样:

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
Run Code Online (Sandbox Code Playgroud)