Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows Service doesn't start after installation

I have built one windows service that sends out email after every 30 minutes in C#. The Service start mode is set to Automatic. But still windows doesn't start automatic. I need to manually start by going to Services.msc and right clicking the service and select start

like image 868
xorpower Avatar asked Dec 04 '25 09:12

xorpower


2 Answers

When the StartMode is set to automatic, that just means that it will start up when Windows boots up.

You can start the service yourself in a custom action in your installer. I assume you have an Installer class already and that it is already a custom action for your setup project since the service is installing, but not starting.

Override the OnAfterInstall method in the Installer class you have and you can start the service like this:

protected override void OnAfterInstall(IDictionary savedState) {
    base.OnAfterInstall(savedState);

    ServiceController sc = new ServiceController(“MyServiceName”);
    sc.Start();
}

However, a scheduled task is not a bad way to go.

like image 116
dontangg Avatar answered Dec 05 '25 22:12

dontangg


Why put yourself through all the overhead and suffering of troubleshooting and maintaining a windows service for a time based/polling application? The windows OS has built in support for this. Just create a simple console application. Run it as a scheduled task.

You should already have unit tests and decoupling to make the code unit-testable. If you don't your troubleshooting is overly difficult. Once you have your code in this unit-testable format flipping to a console app is a no brainer.

I knew a guy who made everything a windows service and labeled it SOA. Piling up windows services for polling/time based mechanisms isn't SOA. Its so sloppy compared to console applications and so much more difficult to maintain I can't even begin to express how bad an idea it is. I had to deal with about ~20-30 of these win services and once they were converted to n-tier and a console app suddenly the stability of the application went through the roof and my life got 10x easier. So please, do yourself a favor and listen to somebody who has been through months and many iterations of these types of app. Run it as a scheduled task in a console app.

like image 22
P.Brian.Mackey Avatar answered Dec 05 '25 22:12

P.Brian.Mackey