Question

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?


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!

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top