I have this method that gets data from an api and saves it to a JSON file.
How can I get the JSON file to be updated every hour on the hour.
public void saveUsers()
{
string uri = "https://****dd.harvestapp.com/people";
using (WebClient webClient = new WebClient())
{
webClient.Headers[HttpRequestHeader.ContentType] = "application/json";
webClient.Headers[HttpRequestHeader.Accept] = "application/json";
webClient.Headers[HttpRequestHeader.Authorization] = "Basic " + Convert.ToBase64String(new UTF8Encoding().GetBytes(usernamePassword));
string response = webClient.DownloadString(uri);
File.WriteAllText(jsonPath, response);
}
}
Make it a console application and use Windows Task Scheduler to call it with any frequency you want.
Use timer, add
object source, ElapsedEventArgs e
parameters in yoursaveUsers()
method and make itstatic
Update
Suppose you have a
MVC
controller nameHome
then you can start the timer from theindex
methodAnd keep one thing in mind because you want to run the timer in an hour interval so may garbage collector stop the timer before method call, you need to keep alive the timer, you can keep timer alive this way after starting the timer