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
Related
How can I directly read a value from Windows Service with VB6?
I wrote a Windows Service with C# which generate a string and I want to read this value with VB6.
For example, Windows Service Application generate a string variable like this for each 10 second:
string id = "1422144";
after 10 second
string id = "2462778";
I want to read this values with VB6 each 10 second. I can read it from text file when I create and write this value to text with Windows Service but I don't want to create a text file. Thanks in advance.
To use MSMQ (Message Queue) approach (note, I've not checked this exhaustively so it's just to get started):
https://msdn.microsoft.com/en-us/library/ms973860.aspx
http://www.informit.com/articles/article.aspx?p=131272&seqNum=6
To create self-hosted OWIN Web Api 2.0 service, I'd recommend using TopShelf (I've done this myself, but too much overhead for just passing a simple string along):
https://codeopinion.com/self-host-asp-net-web-api-as-a-windows-service/
Other options:
Communicate via TCP/IP directly
Use a database (server updates, VB6 reads)
use compicated system of arduino-activated relays to cause mice to take particular paths in a maze, and detect their movements (note: not entirely serious)
Lots of things I've not thought of
I have a question concerning redis in a distributed architecture.
Assume I have n clients, either windows desktop applications or asp.net web/web api servers.
One of the clients, lets say client A, hits the cache for a data and has a miss (the data is not in the cache). The client then starts to get the real data (from lets say a database) and then sets it in the cache when it's done.
Client B comes along and wants the same data, does a fetch to the cache and since it's a miss, does the same processing.
Is there a way for Client B to ...(N) not to do the processing (i.e go to the database) until the data is in the cache and fetch the data from the cache instead when it's available?
I understand that on a single app (or web server), using threads it's easy to check that, but in a distributed architecture?
Is this also a correct way of thinking as well? for the wait process that is
If so then could Client A put a flag somewhere stating that he's loading Data X and that all other clients should wait until he's done?
Otherwise, the idea then would be something along the lines of :
Client A requests Data X
Miss in cache
Processes Data X
Looks if Data X is now in cache
If not, add Data X to cache, otherwise, use it and don't store it in cache
Thanks!
As Kevin said, it's called cache stampede.
One of the best documents to do with this problem I have read is Using memcached: How to scale your website easily (comes from Josef Finsel):
What we need in this instance is some way to tell our program that
another program is working on fetching the data. The best way to
handle that is by using another memcached entry as a lock.
When our program queries memcached and fails to find data, the first
thing it attempts to do is to write a value to a specific key. In our
example where we are using the actual SQL request for the key
name we can just append ":lock" to the SQL to create our new key.
What we do next depends on whether the client supports returning
success messages on memcached storage commands. If it does,
then we attempt to ADD the value. If we are the first one to attempt
this then we’ll get a success message back. If the value exists then
we get a failure indication and we know that another process is trying
to update the data and we wait for some predetermined time
before we try to get the data again.
When the process that’s updating
the cache is done, it deletes the lock key.
IDE: Visual Studio, C# .net 4.0, Winforms application
Is there any way in SQL Server to implement a column in a table which can set itself automatically to 0 when it has not received any ACK (acknowledgement) signal from the application side?
The requirement is I want to keep a column which can keep track that Is application is open or it has been closed?
One way to implement is using the OnClose() event, i.e. on Close() I can change it's value to 0. but the problem is suppose application got hanged for some reason or Power is gone than the value in database will not be updated to zero.
So I want to create an automated column which can track this situation and make itself zero when the application is not sending any request or idle means closed.
please suggest how to handle this.
You can't do that. The only thing you can do is to save GETDATE() in a column in a table as the last activity time of the application and invoke the stored procedure from a high-priority thread every 10 seconds for example.
When you want to know if the application is alive or not, just check this value, if more than 10 seconds is passed since then, you app is gone.
UPDATE:
A more precise but complex approach would be to listen on a socket inside your application and then whenever you want to know if the application is alive, send a request from your sql script to PING the application. You should use CLR programming for this approach, but I think the first one will be practically enough.
Considering it will be a multiple instance scenario where multiple instances of the application can point to same database. You can try the following:
Create a separate table to maintain sessions. This table would contain three columns 1)Machine name or IP 2) Session Id (GUID) and 3) TimeStamp.
Whenever application starts create a new session id and make an entry into this table it means new session is started.
Keep on updating timestamp on every request based on session id.
Have a timeout configured somewhere in web.config or database which will come in handy later.
Now when application is exiting gracefully then delete the row from the table.
Now if application crashes the row won't be delete so next time when application starts you can check if the row already exists. Use machine name or IP to query to the table.
Use the timeout setting configured in (3) above to determine since how long the application is idle.
I'm coding a M2M data capture system using SQL Server 2012 and .net 4.5, the scenario is:
I have a remote data capture app, a web service, a DB.
The app captures data and invoke the web service to upload the data to the DB.
The web service call a "insert" storedproc to write raw data directly in Table A; and then, the web service returns a value telling that the insert was successful or not.
Now, a post-process storedproc needs to be run after the insert process to update another table (Table B).
Previously I used 'job agent' but since the required polling interval changed to 'less than 5 minutes', for the efficiency and real-time reason, I want to avoid to use the 'polling'.
Ideally, I want the app to be able to call the web service and get the return message/value, after that, the DB fires a stored proc to do the post-process work; the work may take longer so the app doesn't need to wait all the processes are done.
Can I fire the post-process sp from DB side? since the DB knows when the insert is done, and it saves communications from outside the DB.
Any suggestions?
You might think of using trigger plus Service Broker. In this way, the trigger will send a message to a queue. service broker will be fired to process the message. It decouples your table A update and table B update. If only use trigger to call table B, it will hold your table A update until the table B update finished.
I have an application that runs as a Windows service. It stores various things settings in a database that are looked up when the service starts. I built the service to support various types of databases (SQL Server, Oracle, MySQL, etc). Often times end users choose to configure the software to use SQL Server (they can simply modify a config file with the connection string and restart the service). The problem is that when their machine boots up, often times SQL Server is started after my service so my service errors out on start up because it can't connect to the database. I know that I can specify dependencies for my service to help guide the Windows service manager to start the appropriate services before mine. However, I don't know what services to depend upon at install time (when my service is registered) since the user can change databases later on.
So my question is: is there a way for the user to manually indicate the service dependencies based on the database that they are using? If not, what is the proper design approach that I should be taking? I've thought about trying to do something like wait 30 seconds after my service starts up before connecting to the database but this seems really flaky for various reasons. I've also considered trying to "lazily" connect to the database; the problem is that I need a connection immediately upon start up since the database contains various pieces of vital info that my service needs when it first starts. Any ideas?
Dennis
what your looking for is SC.exe. This is a command line tool that users can use to configure services.
sc [Servername] Command Servicename [Optionname= Optionvalue...]
more specificly you would want to use
sc [ServerName] config ServiceName depend=servicetoDependOn
Here is a link on the commandlike options for SC.EXE
http://msdn.microsoft.com/en-us/library/ms810435.aspx
A possible (far from ideal) code solution:
In you startup method code it as a loop that terminates when you've got a connection. Then in that loop trap any database connection errors and keep retrying as the following pseudo code illustrates:
bool connected = false;
while (!connected)
{
try
{
connected = openDatabase(...);
}
catch (connection error)
{
// It might be worth waiting for some time here
}
}
This means that your program doesn't continue until it has a connection. However, it could also mean that your program never gets out of this loop, so you'd need some way of terminating it - either manually or after a certain number of tries.
As you need your service to start in a reasonable time, this code can't go in the main initialisation. You have to arrange for your program to "start" successfully, but not do any processing until this method had returned connected = true. You might achieve this by putting this code in a thread and then starting your actual application code on the "thread completed" event.
Not a direct answer put some points you can look into
Windows service can be started Automatically with a delay. You can check this question in SO for some information about it.
How to make Windows Service start as “Automatic (Delayed Start)”
Check this post How to: Code Service Dependencies