3

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

2
  • 1
    What account is the service running under? Commented Dec 23, 2010 at 15:35
  • 1
    Do you mean after you restart the computer to complete the installation? Commented Dec 23, 2010 at 15:36

4 Answers 4

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

this.OnAfterInstall += new System.Configuration.Install.InstallEventHandler(this.ProjectInstaller_OnAfterInstall);
I didn't need the extra += event handler code. Also you can probably get to the name of the service like I did by referencing the control you've placed in the designer. ServiceController sc = new ServiceController(this.serviceInstaller1.ServiceName);
4

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.

1 Comment

yeah windows services are a pain. Sure it feels like your a rockstar developer when your busy writing such technical things [:)] but they are pretty much impossible to debug and take multiple steps to configure and deploy a new version. As stated above, a scheduled task is far superior for the task.
2

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.

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.