A timer for a service to read a table in the DB

0

I need to have a service read a table in the DB every 5 or 10 seconds. I do not see any other way to do that. When this table changes a certain flag, then the service pushes a message to an App. I do not know if this is the best way to automate my App, but that's what's coming up in the head right now and I have no idea how to do it. The lambda that checks the table ok, do not know if the timer or something else, how to do. How do I implement this timer? The idea would be to create a Task (async) that read the table, but the problem is in the timer. Use Api Web with REST and C #.

    
asked by anonymous 29.09.2017 / 20:44

1 answer

1

Using a timer in a web API is not a good idea. I would create another project, windows service type, and then use a timer.

Another option, and this depends on your database, would be to create a trigger that calls your web api in the act of change you expect. For SQL Server One Looks here . In short:

Declare @Object as Int;
Declare @ResponseText as Varchar(8000);

Code Snippet
Exec sp_OACreate 'MSXML2.XMLHTTP', @Object OUT;
Exec sp_OAMethod @Object, 'open', NULL, 'get',
                 'http://www.webservicex.com/stockquote.asmx/GetQuote?symbol=MSFT', --Your Web Service Url (invoked)
                 'false'
Exec sp_OAMethod @Object, 'send'
Exec sp_OAMethod @Object, 'responseText', @ResponseText OUTPUT

Select @ResponseText

Exec sp_OADestroy @Object

Now, honestly, I would never, ever under any circumstances, use a while(true) . Besides being inelegant, and having other alternatives, the web api waits for a first requisition to be started. So your while(true) will only start working after this request, the same applies to the timer.

    
29.09.2017 / 23:25