/// <summary> /// Stop a specific API /// </summary> public void StopApi(HaveApi api) { //stop the server api.Process.Close(); _logger.LogInformation($"Stopping {api.ProjectName} @ {api.Port}"); //remove the server from the dictionary of running servers LaunchedApis.Remove(api); }
/// <summary> /// Starts the API in a new thread /// </summary> /// <param name="api">The Api to start</param> private void StartApi(NeedApi api, int port) { _logger.LogInformation($"Starting Api: {api.ProjectName} @ {port} "); //if LaunchProfile has been set, create dotnet run param for it. var launchProfileArg = "--no-launch-profile"; if (api.LaunchProfile != null) { launchProfileArg = $"--launch-profile {api.LaunchProfile}"; } //configure a background process for running dotnet, //ensuring that the port is set appropriately and //that all console output is to the same console var info = new ProcessStartInfo { FileName = "cmd.exe", Arguments = $"/c dotnet run {launchProfileArg} --no-build --server.urls http://localhost:{port}", //RedirectStandardInput = true, //RedirectStandardOutput = true, //RedirectStandardError = true, //UseShellExecute = false, CreateNoWindow = true, WorkingDirectory = api.LocalProjectDirectory }; //call the dotnet run command asynchronously Task.Run(() => { Process p = new Process(); p.StartInfo = info; //p.OutputDataReceived += new DataReceivedEventHandler(OutputHandler); //p.ErrorDataReceived += new DataReceivedEventHandler(OutputHandler); try { _logger.LogInformation($"trying to start {api.ProjectName} @ {port}"); p.Start(); } catch (Exception ex) { _logger.LogInformation($"EXCEPTION: {ex.Message}"); } //p.BeginOutputReadLine(); //p.BeginErrorReadLine(); _logger.LogInformation($"Starting {api.ProjectName} @ {port}"); //update the console title to add the launched API //lock (_lockObj) { // Console.Title += $", {GetLastSegment(api.ProjectName)}{port}"; //} var haveApi = new HaveApi(_defaultRepoDir) { ProjectName = api.ProjectName, SolutionName = api.SolutionName, Port = port }; //add the launched Api to the dictionary of running APIs haveApi.Process = p; LaunchedApis.Add(haveApi); //wait for the process to be suspended. p.WaitForExit(); }); }