views:

88

answers:

2

Where can I find a detailed view of the lifecycle of a Windows Service as developed in .NET? I put my question this way because I am not sure that a detailed enough description can be posted here, but if you think you can please feel free to try.

An example of an incorrect answer would be a paste of the description from the MSDN page: Introduction to Windows Service Applications. It is not nearly detailed enough. For instance, is a service unloaded out of memory, and therefor the Dispose method is called? Or does it just get stopped by the OnStop method, only to be re-started without initialization by calling the OnStart method?

+2  A: 

The windows service is effectively an application with a few extra methods exposed for the service manager to control it, namely Stop(), Start(), Pause(), Continue() (or equivalents).

When Start is called the application domain is created, the service class initialised and the Start() method called. On stop the Stop() method is called before the application domain is unloaded from memory.

You can see this with task manager. The application doesn't exist in memory until the start is called and it disappears after the Stop is completed.

Therefore I believe that the answer to your lifecycle question lies in the lifecycle of a standard .NET application, be it command line, winforms or asp.net.

I would also advise though that if you are dependent on the Dispose method then there is a probably a flaw lieing somewhere in your design, under most circumstances the resources cleaned up by a Dispose should be disposed more frequently than when the Service Host calls your component to Dispose. Most services are mearly a mechanism for responding to a system event somewhere, in the cases where this event comes from an unmanaged resource, you probably only want to grab the resource OnStart and release it OnStop, in situations where the event is not originating in unmanaged space then you probably want to grab and release the unmanaged resources in a more JustInTime type manner where by you grab them as a resource only when you need them and release them (via their Dispose method) as soon as you can. For further reading check out When and how to use dispose and .Net dispose pattern

David McEwing
This is a very good answer, there is only one point I would like clarified. You state "if [I] am dependent on the Dispose method there there is probably a flaw...in [my] design" I am a little confused by this. Does the instance of ServiceBase hosting my service not call Dispose? Currently I use New() to initialize my objects, OnStart to set them up and 'start them' so to speak, OnStop to stop them, and Dispose to call the Dispose methods for my components, managed objects, unmanaged objects and 'set large fields to null'. I was under the impression that this is what Dispose is to be used for.
Charles Y.
Your right. I've got my Dispose and Finalise muddled, however my intention was still correct, I have modified the last paragraph to match.
David McEwing
Thanks for the clarification.
Charles Y.
A: 

Due to the fact that my question has been answered, and presents another question at the same time, here are some references to object lifecycles' (which I now know also applies to services) for use by future visitors to this question:

StackOverflow - What is the .NET object life cycle?

tutorials.beginners.co.uk/read/id/188

developerfusion.com/article/1047/new-objectoriented-capabilities-in-vbnet/3/

Enjoy!

Charles Y.