Windows Service

  • Thread starter Thread starter Adam Klobukowski
  • Start date Start date
A

Adam Klobukowski

I'm writing a windows service that depends on sql server running. I
could add dependency on sql server in service setup, but it can be
running on different machine. Can I block in OnStart till sql server is
operational?
 
I would not recommend it .. I would instead start a thread from onstart that
then blocked until SQLServer was up and running .. maybe logging an error
every few minutes saying it could not get to the SQL Server.

Cheers,

Greg Young
MVP - C#
http://geekswithblogs.net/gyoung
 
I just did this two days ago. Blocking OnStart causes the MMC to not report
your service's status accurately, which might cause your users to do things
out of frustration like continually restarting the service or stopping it
while you're still trying to make a connection. What I did was use the
ThreadPool to QueueUserWorkItem that made my Sql connection. That way I
could avoid blocking the OnStart method.

I wouldn't suggest adding some sort of polling to retry the connection if it
fails. Generally, when a sql connex fails, its because the thing isn't
configged correctly. Polling is also a no-no in a service. Instead, I
dropped a message in the Event Log (EventLog.WriteEntry) with a description
of the problem and halted the service.
 
Back
Top