简体   繁体   English

Windows服务:检查基本服务是否正在运行

[英]Windows service: check if base service is running

I have created a window service which connects to SQL server. 我创建了一个连接到SQL Server的窗口服务。

But the problem is when Server restarts, my service try to connect to SQL server before SQL server service starts and due to which my service throws exception. 但是问题是服务器重新启动时,我的服务尝试在SQL Server服务启动之前连接到SQL Server,并且由于该原因我的服务会引发异常。

Is there any way to detect if base service(SQL server) is running before my service starts. 在我的服务启动之前,有什么方法可以检测基本服务(SQL Server)是否正在运行。

I have seen here but this is for installation time only. 我在这里看到过,但这仅用于安装时间。

You can define SQL Server to be a dependency of your service, but that does not solve the problem. 您可以将SQL Server定义为服务的依赖项,但这不能解决问题。 The SQL Server service reports to be in "running" status very quickly. SQL Server服务报告非常快地处于“运行”状态。 However when trying to connect you will get an exception, because it is still busy internally. 但是,当尝试连接时,您会得到一个异常,因为它在内部仍然很忙。

There seems to be no reliable way other than just retrying to connect to the database until it finally succeeds. 除了尝试重新连接数据库直到成功之前,似乎没有其他可靠的方法。

See also this question 另请参阅此问题

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

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