xorpower
xorpower

Reputation: 18963

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

Upvotes: 3

Views: 4549

Answers (4)

dontangg
dontangg

Reputation: 4809

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.

Upvotes: 5

Hans Passant
Hans Passant

Reputation: 941218

Auto-starting services are subject to problems with service initialization order. You have plenty of dependencies, the TCP/IP stack better be in working order before you try to send an email, for example. Look in the Windows event log for an exception message that prevented OnStart() from getting the service started.

This can be configured for a service, check out the Dependencies tab for the Print Spooler service for example. This is however difficult to deal with, hard to figure out exactly which services need to be running and hard to write the registry entries that configure the dependencies.

Punt the problem: don't send an email message right away. Wait a while, 30 minutes for example.

Upvotes: 2

P.Brian.Mackey
P.Brian.Mackey

Reputation: 44275

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.

Upvotes: 4

fejesjoco
fejesjoco

Reputation: 11903

You install it with installutil? You're right, it doesn't start the service, even if it's set to automatic. If I were you, I'd provide a batch file which calls installutil and then also runs 'net start whatever'. Or if you're using other kinds of installation, those should provide this ability too.

Upvotes: 0

Related Questions