Thursday, June 6, 2013

Interview Series: "What is a Windows Service and how does its lifecycle differ from a "standard" EXE?"

I came across a post on Scott Hanselman's website titled 'What Great .NET Developers Ought To Know (More .NET Interview Questions)' and the list of questions really caught my interest. So, I've decided to try and answer all of the questions. I'll probably have to look up a bunch, and some of the questions aren't really applicable to my field of work, but still - I'm expecting to learn quite a bit.

"What is a Windows Service and how does its lifecycle differ from a "standard" EXE?"

A windows service is a program that runs in the background, whereas a program runs mostly in the foreground. Again - go over to WikiPedia for more details. There are other differences as well: the way you start it, the way you install it etc.
Both can be created using Visual Studio 'file > new project' and there's not a whole lot to it. And - addressing the lifecycle part of the question: a service normally runs when windows starts and stop when windows shuts down. The application lifecycle however is normally controlled by the user, that can either start or stop it at will.

So when to opt for a service and not for an application? Well, when you need just that kind of behavior as described above. Something that always runs on the background and needs little or no user input. 

For example: I was working on a bookkeeping application in my former job. We needed to send out tax declarations to the authorities. We found a really cheap application that did just that - read from a pick-up folder and send it to the tax authorities. So - we decided to write a service that queried the database for declarations flagged to be sent out, every n minutes, and copy them to the pick-up location of the application.
This service started immediately when the machine booted, so it continued as the machine came back up.

Since the service was important however - we did ask our full service hosting provider to monitor the service state using nagios. As soon as that service went down, we would get a phone call. If the service is important to you - consider having that done!

No comments:

Post a Comment