简体   繁体   中英

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.

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.

No you cannot. Well, you could , but you really should not . A webservice is on demand . 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. 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.)

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:

  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)

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

for your service you can use web api, web method or wcf, it is depend on you


If you use hangfire, quartz or any other scheduler you can set a background job on your server to check your database status

"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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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