我知道的唯一方法是在 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());
}
}
}
}
}
}
然后您将指示 Visual Studio 不使用 Cassini。转到您的 Web 应用程序的属性 -> Web 并选择“使用自定义 Web 服务器”,输入如下内容:(http://localhost:1685/
或您想使用的任何端口号)。然后,在构建后事件中输入此命令:
"$(ProjectDir)..\SpawnProc\bin\debug\SpawnProc" "C:\Program Files (x86)\Common Files\microsoft shared\DevServer\9.0\WebDev.WebServer.exe" 1685 "$(ProjectDir)" /
确保您的路径正确,例如,由于我运行的是 64 位操作系统,我的程序文件路径与 32 位操作系统不同。另外,我的 SpawnProc.exe 在一个子项目中。