Click here to Skip to main content
15,900,108 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm in the middle of writing an API that routinely receives information from a POST request every minute. However, I want to make it so my API is able to detect when it doesn't receive this POST request from client every 30 minutes. To make it a bit clearer, I want to make it so, if for some reason, the client stops sending POST requests for 30 minutes, my API will run a function. How would I go about implementing this periodic POST request check? I assume I would have to do some loop where I schedule a re-occuring event however I'm not sure as to how to set it up because I'm still very new to C#.

What I have tried:

This is all the code for my basic API
[ApiController]
    [Route("[controller]")]
    public class GeneratorStatusController : ControllerBase
    {

        static List<string> strings = new List<string>()
        {
            "value0", "value1", "value2"
        };

        static CheckForInput()
        {
            /// Check to see if I've received any POST requests
            /// If I haven't received one for 30 minutes, send out 
            /// an email 
        }

        [HttpGet]
        public List<string> GetValues()
        {
            return strings;
        }

        [HttpGet("{id}")]
        public string Get(int id)
        {
            return strings[id];
        }

        [HttpPost("{time}")]
        public void Post(string time)
        {
            strings.Add(time);
        }
Posted
Updated 24-Sep-20 6:02am

1 solution

You don't do it in the web API. Web API's have the same limitations as normal HTTP traffic. It's a multi-user request/response system.

This would require setting up some state tracking for each client session. A client connects, the API assigns an ID to that client session and keeps it in a database. Every time the client calls the API, the API method updates the session state and timestamp of the last call in the database.

Now, the API does not check the database to see which sessions haven't been updated. That would be left up to a separate process (Windows service or scheduled job) to poll the database to check to see if each session has been updated in the time allotted. That process would then either call another method in the API or handle the situation itself where you do whatever it is you need to do, and remove the session database from the database if business rules require it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900