So this is a funny one. I have a dotnetcore 3 project using EntityFrameworkCore and VueJS. Right. I have in my Startup.cs:Configure(app,env) a statement that looks as follows:
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);
}
});
As you can see I have manually specified the port to be 4444 if I didn't do this the application would try and run on port 8080 or 8080++ until the application would hit a free port on the machine. This is ok for when you would only run the application once and then it never crashed or Visual Studio never crashed or you'd have to stop debugging your code to manage your project filestructure.. You get my point.. In development you NEVER only just fire a command once and be done with it. Not in my experience at least.
But nonetheless this is what this setup assumes that you do, because it never stops the npm-script when the IIS Express service shuts down, but it attempts to relaunch the, in my case, VueJS application, on the same localhost port, which ensures that the connection to the relaunched application is not established correctly and thus mayhem ensues. Unless you kill the previous npm-script task through the windows terminal that is.
So my question here is - does anyone know how to stop the script from running and killing the task that keeps it alive, when I stop my IIS Express server? This is kinda baffling to me to be honest :)
The middleware class if anyone is interested :)
#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);
}
}