简体   繁体   English

Web服务检查数据库是否可用

[英]Web Service checking that DB is available

I need to create an ASP.NET web service that simply returns an information telling if a database is available or down when it's consumed. 我需要创建一个ASP.NET Web服务,该服务仅返回一条信息,告知使用数据库时数据库是否可用或是否关闭。

So I would like to know if I can set a task that is executed inside the web service method on a regular basis to check the connection to the database and return the result via a URL. 因此,我想知道是否可以设置一个在Web服务方法内部定期执行的任务,以检查与数据库的连接并通过URL返回结果。

No you cannot. 你不能。 Well, you could , but you really should not . 嗯,你可以 ,但你真的 应该。 A webservice is on demand . Web服务是按需的 If it's called, it does work. 如果调用,它确实可以工作。 If it's not called, it's not running. 如果未调用,则表明它没有运行。

You may have been thinking of a windows service. 您可能一直在考虑Windows服务。 That is something that is always running and can do stuff in the background. 是一直在运行的事情,可以在后台做一些事情。

(A windows service may have an additional web frontend to see it's data. Or any other way to visualize it's data points, for example another database.) (Windows服务可能具有一个额外的Web前端来查看其数据。或任何其他可视化其数据点的方式,例如另一个数据库。)

As you tagged c# and asp so you are using Sql server database, this query gives you databases that exists on your sql server instance: 在标记c#和asp以便使用Sql服务器数据库时,此查询将为您提供sql服务器实例上存在的数据库:

  select * from master.dbo.sysdatabases

result contains name and some extra information, name gives you database names, mode column have a int value indicates that database is in creating mode or created mode, status column that contains a int value(power of 2) 结果包含名称和一些其他信息,名称为您提供数据库名称,模式列具有int值,指示数据库处于创建模式或已创建模式,状态列包含int值(2的幂)

If you would like to see if database is offline or not, you can see status value if it is 512 your database is offline, for 1024 it is in read only mode 如果要查看数据库是否处于脱机状态,则可以查看状态值(如果数据库为512,则数据库处于脱机状态,对于1024,它处于只读模式)

for your service you can use web api, web method or wcf, it is depend on you 为了您的服务,您可以使用Web API,Web方法或WCF,这取决于您


If you use hangfire, quartz or any other scheduler you can set a background job on your server to check your database status 如果使用hangfire,quartz或任何其他调度程序,则可以在服务器上设置后台作业以检查数据库状态

"return the result via a URL" I can not understand this, but if you want to notify users about database you can use push notification on your background job “通过URL返回结果”我无法理解,但是如果您想通知用户有关数据库的信息,则可以在后台作业中使用推送通知

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

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