简体   繁体   English

唤醒Windows服务中运行的WCF服务

[英]Wake up WCF service running inside a windows service

I've created a simple WCF service which is hosted within a windows service. 我创建了一个简单的WCF服务,该服务托管在Windows服务中。 WCF service is automatically started on windows service start, but when I connect to the WCF service from a client for the first time it takes a couple of seconds (as any other WCF service). WCF服务会在Windows服务启动时自动启动,但是当我第一次从客户端连接到WCF服务时,它会花费几秒钟的时间(与其他WCF服务一样)。

I'm aware on how to avoid this delay when the service is hosted on IIS, but not when it's hosted within anything else. 我知道如何在IIS上托管服务时避免这种延迟,但在其他任何地方托管时却无法避免。

To be exact: 确切地说:

Is there a way to wake-up the WCF service which is hosted within a windows service without a client reqest being made to it? 有没有一种方法可以唤醒Windows服务中托管的WCF服务,而无需客户端请求? Possibly from the windows service code? 可能来自Windows服务代码?

With IIS hosting, the delay is not because the WCF service needs to "wake up". 使用IIS托管,延迟不是因为WCF服务需要“唤醒”。 The reason this happens in IIS hosting is that IIS will unload the app pool from memory after a period of inactivity (or due to some other internal state). 在IIS托管中发生这种情况的原因是,一段时间不活动(或由于某些其他内部状态)后,IIS将从内存中卸载应用程序池。 This does not apply with windows service hosting. 这不适用于Windows服务托管。

The delay you are experiencing is likely because when your client calls the service for the first time it must spin up a wcf channel to make the call. 您遇到的延迟很可能是因为您的客户端在首次呼叫该服务时必须旋转一个wcf频道才能进行呼叫。 Subsequent calls can then be made using the same channel so there is no lead time. 然后可以使用相同的通道进行后续呼叫,因此没有提前期。

To prevent this behavior you must keep the channel open all the time. 为避免这种情况,您必须始终保持通道打开。 There are several ways to do this, but the simplest is to use sessions. 有几种方法可以做到这一点,但最简单的方法是使用会话。 See Erik Funkenbusch's excellent answer for more information. 有关更多信息,请参见Erik Funkenbusch的出色答案 However, you should consider if you absolutely need this, as sessions are an overhead and don't scale well. 但是,您应该考虑是否绝对需要此功能,因为会话是一项开销,并且扩展性不好。

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

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