0

I have a few Windows services (all written in C#) that all show the same strange behaviour. I have them set to delayed auto start so that they get started after the boot (delayed because well they are not critical). They all host WCF services as parts of Client-Server applications and were installed using WiX if that matters.

I noticed that sometimes they just don't start. If you look into the Services window fast enough after the OS is ready they have status "Starting". If you then refresh the view they are no longer starting but not "Started" either. You can then start them manually without any problem whatsoever.

This produces no error messages and no log entrys. And to make it even better this only occurs if the machine has been shut down and turned on again. Reboot works perfectly fine every time (tried it about 20 times on two different machines)

If you set the failure actions to restart the service after failure it seems it will eventually start the service successfully but surely this can not be the ideal solution.

OSs are Windows 7 and WinServer 2008 R2

What am I missing here? Why do they fail to be started automatically(the first time at least)? And why does it make a difference if the computer boots following a reboot or a shutdown?

EDIT: I was wrong about the failure actions. The did not fix the problem.

EDIT 2: I have added exception handling around everything to log possible exceptions. But so far no exceptions have been logged.

mgttlinger
  • 1,435
  • 2
  • 21
  • 35
  • possible duplicate of [Windows Service won't automatically start after reboot](http://stackoverflow.com/questions/3719958/windows-service-wont-automatically-start-after-reboot) – Christopher Painter Jun 05 '13 at 13:29

2 Answers2

2

Might it be the WCF Services take a long time to start? afaik, the windows service has to come up in a certain time (best practices is 30 seconds, technical limit I don't know) to not time out. That could explain why your service is in status "starting" but does not start.

this.myself
  • 2,101
  • 2
  • 22
  • 36
  • Then how would you explaint that the service does successfully start(almost instantly) if I start it manually? – mgttlinger Jun 12 '13 at 09:50
  • maybe any dependencies of your WCF service are not ready at system startup? – this.myself Jun 12 '13 at 11:45
  • If its a dependency problem an exception should be thrown. Or is this not the case? – mgttlinger Jun 12 '13 at 12:37
  • with dependency I think about dependencies to other services like a database service which is might not be ready at startup. If there would be an assembly dependency missing, there would be an exception, yes. – this.myself Jun 12 '13 at 12:47
  • So if a service dependency is not met there is no exception being thrown? – mgttlinger Jun 12 '13 at 13:27
  • Seems like you were right after all. I believe it is kinda strange that no error or anything is logged if the service gets terminated because it takes longer than the set time. I "fixed" the problem by requesting 15 minutes of additional time to start. Certainly not the best solution, but it seems to work. – mgttlinger Jul 24 '13 at 06:06
  • I'm glad to hear you could fix the problem. And better to have a not so clean solution than no solution ;-) But what you could try is to start your wcf services in threads, so the OnStartup method of the windows service would return fast and not wait until the wcf serives are up. For the lack of logging: Make sure you have the property "AutoLog" set to "true" in your windows service class (the one inheriting from ServiceBase). But I'm not sure, if your issue would be logged at all. – this.myself Jul 24 '13 at 06:40
  • Yes but I have other programms waiting for the service to be running. So if my OnStart method returns and the service is not yet functional this wouldn't be an ideal situation. – mgttlinger Jul 24 '13 at 07:28
  • Ok I yust found out the service does not - as it first seemed - always start even with the additional requested time. But it appears to happen less frequent now. And as always no sign of a crash whatsoever. – mgttlinger Aug 16 '13 at 06:49
0

Please see my answer from the duplicate. A windows service typically shouldn't have access to the desktop for security reasons. But it certainly should have a good amount of logging in it. You probably have a race condition. The only thing you could do about this in WiX would be to express a dependency on another service to get the service control manager to wait awhile before starting the service. But it really would be better if your code was more robust. An example would be the OnStart event fire up a background worker process and then return success. The background thread could then keep attempting to host the WCF endpoint and everything do a fair amount of logging in the process.

Christopher Painter
  • 54,556
  • 6
  • 63
  • 100
  • Wouldn't an exception get thrown if my code tries to access something that should be provided by another service that is not yet started? – mgttlinger Jun 05 '13 at 13:56
  • Not sure how to answer without looking at your code. Put in a try catch and log for it. It's kind of like the old question "If a tree falls in a forest and no one is around to hear it, does it make a sound?" – Christopher Painter Jun 05 '13 at 14:00
  • But aren't exceptions logged automatically by default? – mgttlinger Jun 05 '13 at 14:05
  • It should be, if you aren't catching them and know where to look in event viewer. Not the idea story IMO though. http://stackoverflow.com/questions/711032/diagnosing-clr-errors-in-windows-event-viewer – Christopher Painter Jun 05 '13 at 15:53
  • Now I have added trys and catches around everything and log the exception. But no exception occurs so nothing is logged. – mgttlinger Jun 06 '13 at 15:40