I have a Windows service and an MSI installer (setup project) for it. The setup project has custom actions for install and uninstall with args of /install and /uninstall respectively.
I would like the service to start immediately after the install. All my service does is starts a process. When the service is stopped, it does process.Close();.
I have tried doing
var sc = new ServiceController( "MyProcess" );
sc.Start();
The process starts, but the event log message saying the service has started doesn’t show. When stopping I get the error Error stopping process: System.InvalidOperationException: No process is associated with this object..
If I don’t use the service controller to start and use Services.msc instead, it works as expected when stopping.
Is there a way to have the process start immediately and have my start/stop methods work as expected?
I ended up taking a different approach by just getting the running process instead of trying to hold on to the one started before. Stopping the process then works just fine, and doesn’t matter of the OnStart/OnStop methods are functioning “properly”, because my inner process is starting/stopping like I need.