Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where can I find a detailed view of the lifecycle of a Windows Service as developed in .NET?

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!

like image 899
Charles Y. Avatar asked Jul 16 '09 20:07

Charles Y.


People also ask

Where do I find Windows services?

Press the Win + R keys on your keyboard, to open the Run window. Then, type "services. msc" and hit Enter or press OK. The Services app window is now open.

What is Windows Service in .NET Core?

An ASP.NET Core app can be hosted on Windows as a Windows Service without using IIS. When hosted as a Windows Service, the app automatically starts after server reboots.

What is Windows Service in VB net?

The core function of a Windows Service is to run an application in the background. A few things that make them different from a Windows application are that a Windows Service starts before any user logs on to the system (if it has been set up to start at boot up).


1 Answers

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

like image 78
David McEwing Avatar answered Nov 14 '22 21:11

David McEwing