在哪里可以找到用 .NET 开发的 Windows 服务生命周期的详细视图?

发布于 2024-07-27 03:40:28 字数 701 浏览 4 评论 0原文

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

错误答案的一个示例是 MSDN 页面中的描述粘贴: Windows 服务应用程序简介。 它还不够详细。 例如,服务是否被卸载到内存不足,因此调用了 Dispose 方法? 或者它只是被 OnStop 方法停止,只是通过调用 OnStart 方法重新启动而不进行初始化?


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

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

tutorials.beginners.co。 uk/read/id/188developerfusion.com/article/1047/new-objectoriented-capability-in-vbnet/3/

享受

吧!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鹿童谣 2024-08-03 03:40:28

Windows 服务实际上是一个应用程序,它公开了一些额外的方法供服务管理器控制它,即 Stop()Start()Pause()Continue()(或等效项)。

当调用 Start 时,将创建应用程序域、初始化服务类并调用 Start() 方法。 停止时,在从内存中卸载应用程序域之前调用 Stop() 方法。

您可以使用任务管理器看到这一点。 在调用 start 之前,应用程序并不存在于内存中,并且在 Stop 完成后消失。

因此,我相信您的生命周期问题的答案在于标准 .NET 应用程序的生命周期,无论是命令行、winforms 还是 asp.net。

我还建议,如果您依赖 Dispose 方法,那么您的设计中可能存在缺陷,在大多数情况下,Dispose 清理的资源应该比服务主机调用您的组件时更频繁地处理处置。 大多数服务只是一种响应某处系统事件的机制,在该事件来自非托管资源的情况下,您可能只想在 OnStart 时获取资源并在 OnStop 时释放它,在事件不是源自非托管资源的情况下空间,那么您可能希望以更 JustInTime 类型的方式获取和释放非托管资源,即仅在需要时将它们作为资源获取,并尽快释放它们(通过其 Dispose 方法)。 如需进一步阅读,请查看何时以及如何使用 dispose.Net 处置模式

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文