我正在按照此示例创建自托管 WCF 服务。理想情况下,如果某个值在数据库中更新,我希望服务与计时器连接每半小时检查一次,如果是,服务将执行一些任务,否则将每半小时检查一次。我在网上读到过,在 IIS 托管的 WCF 中使用计时器不是一个好主意,在自托管的 wcf 服务上使用它怎么样?有什么例子吗?
谢谢,
我认为对您来说更好的选择是创建一个简单的控制台应用程序,如果值更新则执行您的任务,然后在 Windows 中创建一个计划任务,每半小时运行一次此控制台应用程序。这样,您可以让 Windows 管理计时部分,您只需编写检查数据库并在必要时更新它的代码。
不确定您运行的是哪个版本的 Windows,但您可以从控制面板访问计划任务。
The reason a timer in a IIS hosted WCF service is "not a good idea" is a IIS service has a much different lifetime than a self hosted service. See this SO question and answer for some details and this MSDN article for even more details.
Basically a WCF service can be "shut down" while being hosted inside IIS if no-one has connected to it within a timeout period. If you need regular periodic maintenance like you are describing you will need to use a self hosted service and have that service start a timer up that fires every half hour in it's OnStart()
call.