5

在哪里可以找到以 .NET 开发的 Windows 服务生命周期的详细视图?我这样提出我的问题是因为我不确定是否可以在此处发布足够详细的描述,但是如果您认为可以,请随时尝试。

不正确答案的一个示例是粘贴 MSDN 页面中的描述:Introduction to Windows Service Applications。它还不够详细。例如,一个服务是否从内存中卸载,因此调用了 Dispose 方法?或者它只是被 OnStop 方法停止,只是通过调用 OnStart 方法重新启动而无需初始化?


由于我的问题已得到回答,并同时提出了另一个问题,这里有一些对对象生命周期的引用(我现在知道这也适用于服务),供未来的访问者使用这个问题:

StackOverflow - .NET 对象生命周期是什么?

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

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

享受!

4

1 回答 1

8

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

于 2009-07-16T20:47:58.300 回答