所以这很有趣。我有一个使用EntityFrameworkCore和VueJS的dotnetcore 3项目。对。我的Startup.cs:Configure(app,env)
中有一个语句如下:
app.UseSpa(spa =>
{
if (env.IsDevelopment())
spa.Options.SourcePath = "ClientApp";
else
spa.Options.SourcePath = "dist";
if (env.IsDevelopment())
{
spa.UseVueCli(npmScript: "serve", port: 4444, forceKill: true);
}
});
如您所见,我没有手动将端口指定为4444
,应用程序将尝试在端口8080
或8080++
上运行,直到应用程序碰到计算机上的空闲端口。当您只运行一次应用程序,然后它从未崩溃或Visual Studio从未崩溃,或者您必须停止调试代码来管理项目文件结构时,这是可以的。只需触发一次命令即可完成。至少不是我的经验。
但是尽管如此,该设置仍假定您要做,因为它在IIS Express服务关闭时从不停止npm-script,但它尝试在同一本地主机端口上重新启动VueJS应用程序,这样可确保未正确建立与重新启动的应用程序的连接,从而导致混乱。除非您通过Windows终端杀死先前的npm-script任务。
所以,我的问题是-当我停止IIS Express服务器时,有人知道如何停止运行脚本并终止使脚本保持活动的任务吗?老实说,这有点让我困惑:)
中间件类,如果有人感兴趣的话:)
#region Assembly VueCliMiddleware, Version=3.1.0.0, Culture=neutral, PublicKeyToken=null
// C:\Users\<USER>\.nuget\packages\vueclimiddleware\3.1.1\lib\netcoreapp3.1\VueCliMiddleware.dll
#endregion
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.SpaServices;
using VueCliMiddleware;
namespace VueCliMiddleware
{
public static class VueCliMiddlewareExtensions
{
public static IEndpointConventionBuilder MapToVueCliProxy(this IEndpointRouteBuilder endpoints, string pattern, SpaOptions options, string npmScript = "serve", int port = 8080, ScriptRunnerType runner = ScriptRunnerType.Npm, string regex = "running at", bool forceKill = false);
public static IEndpointConventionBuilder MapToVueCliProxy(this IEndpointRouteBuilder endpoints, string pattern, string sourcePath, string npmScript = "serve", int port = 8080, ScriptRunnerType runner = ScriptRunnerType.Npm, string regex = "running at", bool forceKill = false);
public static IEndpointConventionBuilder MapToVueCliProxy(this IEndpointRouteBuilder endpoints, SpaOptions options, string npmScript = "serve", int port = 8080, ScriptRunnerType runner = ScriptRunnerType.Npm, string regex = "running at", bool forceKill = false);
public static IEndpointConventionBuilder MapToVueCliProxy(this IEndpointRouteBuilder endpoints, string sourcePath, string npmScript = "serve", int port = 8080, ScriptRunnerType runner = ScriptRunnerType.Npm, string regex = "running at", bool forceKill = false);
public static void UseVueCli(this ISpaBuilder spaBuilder, string npmScript = "serve", int port = 8080, ScriptRunnerType runner = ScriptRunnerType.Npm, string regex = "running at", bool forceKill = false);
}
}
确定,免责声明:这是一个完全的破解,仅当您事先知道端口(至少,在我的机器上可以工作的计算机;)时才有效,需要HEAVY重构,而且我不做任何保证。
很少有人知道您可以很容易地在Startup类中附加到应用程序生命周期:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime lifetime)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
lifetime.ApplicationStopping.Register(OnStopping); // <== Magic is here!
}
// [Blah blah, yada yada, your super cool code here]
}
private void OnStopping()
{
// Things to do before application is stopped
}
[我的想法是得到正在监听您的known tcp port
(比方说4444
)和kill it
(戏剧性音乐)的过程。
我知道从端口检索PID的更快方法是使用netstat -nao
。 OnStopping代码将如下所示:
private void OnStopping()
{
Process netstat = new Process();
netstat.StartInfo = new ProcessStartInfo
{
FileName = "netstat",
Arguments = "-nao" // | find \"127.0.0.1:4200\""
,
RedirectStandardOutput = true
};
netstat.Start();
var output = netstat.StandardOutput.ReadToEnd();
var ports = output.Split('\n');
var f = ports.FirstOrDefault(p => p.Contains("LISTENING") && p.Contains("127.0.0.1:4200"));
var pid = int.Parse(f.Split(' ').Last());
var clientServer = Process.GetProcessById(pid);
clientServer.Kill();
clientServer.WaitForExit();
}
请让我知道您的想法,如果它适合您。