I built an ASP.NET Core 6 MVC app and was able to successfully build and run it as an electron app. Part of my app's UI hits a controller endpoint solely to start/stop a companion Windows Service.
I am receiving the below error message when doing so. I confirmed my service name matches in the service console vs in my code so the error message doesn't make a ton of sense. I did confirm this works as expected running as a standard web app.
Any ideas if this is possible in this way or do I need to open up an IPC channel to do this functionality?
public IActionResult StartService()
{
try
{
using (ServiceController serviceController = new ServiceController("SERVICENAME"))
{
if (serviceController.Status == ServiceControllerStatus.Stopped)
{
serviceController.Start();
serviceController.WaitForStatus(ServiceControllerStatus.Running);
}
}
return Content("Service started");
} catch (Exception ex)
{
return Content($"Error starting service: {ex}");
}
}
public IActionResult StopService()
{
try
{
using (ServiceController serviceController = new ServiceController("SERVICENAME"))
{
if (serviceController.Status == ServiceControllerStatus.Running)
{
serviceController.Stop();
serviceController.WaitForStatus(ServiceControllerStatus.Stopped);
}
}
return Content("Service stopped");
}
catch (Exception ex)
{
return Content($"Error stopping service: {ex}");
}
}
Here is the error message returned in electron dev console
Error starting service: System.InvalidOperationException: Cannot start service 'SERVICENAME' on computer '.'.
System.ComponentModel.Win32Exception (2): The system cannot find the file specified.
--- End of inner exception stack trace ---
at System.ServiceProcess.ServiceController.Start(:8001/String[] args)
at :8001/System.Service…Controller.Start()
at :8001/PROJECT.Cont…troller.cs:line 207