简体   繁体   English

WCF服务主机中的计时器

[英]Timer in WCF service host

Is it a good idea to initialize a timer to do a periodic task in a WCF service host or will that mess with the lifecycle or performance of the service being hosted? 初始化计时器以在WCF服务主机中执行定期任务是个好主意,还是会干扰正在托管的服务的生命周期或性能?

I have a custom service host that announces it's availability (with the goal of creating a registry for clients) upon starting and stopping. 我有一个自定义服务主机,该主机在启动和停止时宣布其可用性(目的是为客户端创建注册表)。 I want to create a timer to do this periodically but my concern is that this will cause problems (I haven't ran into any yet but maybe I haven't stressed it enough or something) or maybe it will eat resources and kill the performance of the service. 我想创建一个计时器来定期执行此操作,但是我担心这会导致问题(我还没有遇到任何问题,但也许我还没有对此施加足够的压力),或者它会占用资源并破坏性能服务。 The idea is to hookup the timer on the OnOpened and OnClosing events, of course. 当然,其想法是在OnOpened和OnClosing事件上连接计时器。

For what is worth, this service is hosted in IIS so it is IIS the one instantiating and managing the service host. 值得一提的是,此服务托管在IIS中,因此IIS是实例化和管理服务主机的一个。

Is it a good idea to initialize a timer to do a periodic task in a WCF service host or will that mess with the lifecycle or performance of the service being hosted? 初始化计时器以在WCF服务主机中执行定期任务是个好主意,还是会干扰正在托管的服务的生命周期或性能?

That will depend on where your WCF service is hosted. 这将取决于WCF服务的托管位置。 If it is hosted inside IIS it might be problematic. 如果将其托管在IIS中,则可能会出现问题。 The reason for this is because IIS can decide to recycle the ASP.NET application pool at any time and your timer will simply stop working. 这样做的原因是因为IIS可以随时决定回收ASP.NET应用程序池,而您的计时器只会停止工作。 It is not something you can rely upon. 您不能依靠它。 If on the other hand you have hosted your WCF service inside a Windows Service self host, it is fine to use a timer. 另一方面,如果您已将WCF服务托管在Windows服务自身主机中,则可以使用计时器。 You may take a look at the following blog post about the dangers of implementing recurring background tasks in ASP.NET hosts. 您可以查看following blog post有关在ASP.NET主机中实现重复执行后台任务的危险的following blog post

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM