views:

45

answers:

1

I have a WCF service that is running as a windows service. As soon as the WCF service starts it will try to connect to the DB and do some initialization processing.

I'm having an issue when I restart my machine (the wcf service is set to start automatically), the service would get an exception as too sql is busy. So it seems like the sql is trying to start and at the same time my service is trying to communicate with it.

For now, I did something like try/catch and I sleep for like 10 sec in my catch and I retry again until my initialization is successful.

This works pretty well. But I'm looking for a more elegant solution.

I don't want to set a dependency on sql on my windows service as the service can be installed on a different machine that the DB.

+1  A: 

You might try configuring the "Recovery" options of your service when you deploy it to have it restart itself when it fails to load. You can do this via the SC.EXE command-line tool, WMI, or manually through the service properties (services.msc MMC).

Trying [n] times, with some timespan between retries, before ultimately failing if still unable to connect is not a bad solution if you consider monitoring and alerting.

The service actually failing to start will write warning/error events to the Windows event log, potentially triggering monitoring and alerting behavior that you might not want if its just waiting on a dependency to be available, and it will eventually recover.

HTH,

Z

Zach Bonham
Thanks, the recovery is an option its just that there is still a chance that configuration gets forgotten when deploying to client. So I guess I go with trying n times until failing.
pdiddy
Automate the deployment of your service, including configuring any dependency, or recovery options, and then you don't have to worry about forgetting to do it. :)
Zach Bonham
We are using the setup project template from visual studio to build our msi. Is it possible do configure the recovery option with the setup project?
pdiddy
I am sure it is, but I'm not sure how you would go about doing it - we don't install our services using MSI's. We use msbuild to orchestrate tasks on each server (such as copying files, installing services, etc). If you stuck on building MSI's I hear that the WIX project is pretty nice for creating those MSI's. I think this is its project location: http://sourceforge.net/projects/wix/
Zach Bonham