首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在IIS Express停止后停止npm任务/脚本

在IIS Express停止后停止npm任务/脚本
EN

Stack Overflow用户
提问于 2020-02-25 09:18:44
回答 2查看 693关注 0票数 2

所以这是个有趣的故事。我有一个使用EntityFrameworkCore和VueJS的dotnetcore 3项目。正确的。在我的Startup.cs:Configure(app,env)中有一条声明如下:

代码语言:javascript
复制
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,如果没有这样做,应用程序将尝试在端口80808080++上运行,直到应用程序到达机器上的空闲端口为止。这是可以的,当您只运行应用程序一次,然后它从未崩溃,或者Visual从未崩溃,或者您必须停止调试您的代码来管理您的项目文件结构。你明白我的意思..。在开发中,您不可能只发出一次命令并完成它。至少以我的经验来说不是。

但是,尽管如此,这个设置还是假设您所做的,因为当nonetheless服务关闭时,它从不停止npm脚本,但是它试图在相同的本地主机端口上重新启动(在我的例子中是VueJS应用程序),这确保了与重新启动的应用程序的连接不正确地建立起来,从而导致混乱。除非您通过windows终端即windows终端终止了之前的npm-script任务。

所以,我的问题是--当我停止IIS服务器时,有人知道如何阻止脚本运行并杀死使它保持活力的任务吗?老实说,这让我有点困惑:)

中间件类(如果有人感兴趣的话:)

代码语言:javascript
复制
#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);
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-02-28 19:09:11

好的,免责声明:这是一个完全的黑客,只有在您事先知道您的端口(至少在我的机器上工作),需要重型重构时才能工作,而我不提供任何保证。

很少有人知道,在Startup类中,您可以很容易地附加到应用程序生命周期:

代码语言:javascript
复制
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代码如下所示:

代码语言:javascript
复制
private void OnStopping()
{
    // Following code runs on Windows platform only, 
    // if current platform is something else, we just exit
    if(!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) return;

    Process netstat = new Process();
    netstat.StartInfo = new ProcessStartInfo
    {
        FileName = "netstat",
        Arguments = "-nao",
        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:4444"));
    var pid = int.Parse(f.Split(' ').Last());
    var clientServer = Process.GetProcessById(pid);
    clientServer.Kill();
    clientServer.WaitForExit();
}

请告诉我你的想法,如果它对你有用的话。

票数 2
EN

Stack Overflow用户

发布于 2020-02-29 05:47:31

部分回答(某种程度)。来自https://github.com/EEParker/aspnetcore-vueclimiddleware/blob/master/src/VueCliMiddleware/VueDevelopmentServerMiddleware.cs

以下内容如下:

代码语言:javascript
复制
if (portNumber < 80)
{
  portNumber = TcpPortFinder.FindAvailablePort();
}
else
{
  // if the port we want to use is occupied, terminate the process utilizing that port.
  // this occurs when "stop" is used from the debugger and the middleware does not have the opportunity to kill the process
  PidUtils.KillPort((ushort)portNumber, forceKill);
}

您可以在本地构建这个程序,然后跟踪查看它是否/为什么不能像预期的那样工作。似乎forceKill的全部目的就是解决这个问题。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60391141

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档