How to call an action as soon as the Web API starts?

Asked

Viewed 130 times

0

I’m studying Web API and I’m having doubts about how to call an Action as soon as I click the Visual Studio play button.

I imagine this should be done in the Main method of the Program class, but I have no idea how to do this.

The ultimate goal is to get this application started and, from time to time, read the database data and send it to an endpoint of another API that I will still create.

For now, I’m in this phase of making the application start by calling an Action.

Additional information:

Later, I will use the Chroniton package to read the data periodically.

  • Strange your question because it’s clearer ...

  • It doesn’t make sense for a Webapi to call an action of its own... But you can run this routine at the start of the application yes at startup

  • Virgilio Novic and @Leandroangelo what I need is, once I start the API, she reads the table data from time to time and sends it to an endpoint of another API. And I’m racking my brain to know how it should be done.

  • @Evertonsolon From time to time? I don’t think this is appropriate... it may be the case to create another application with this unique responsibility.

  • @Leandroangelo I understand your point of view but it is what the test of the company, in which I am a candidate, is asking. Entede? rsrs

2 answers

1

You can also use Hangfire to schedule the action. https://www.hangfire.io/

Just install the packages and then enter the call to hangfire to make the schedule Example:

var jobId = BackgroundJob.Schedule(
    () => Console.WriteLine("Delayed!"),
    TimeSpan.FromDays(7));
  • Thanks for the tip @Felipe Alexandre Miranda!!! I saw that this package has many more downloads than Chroniton.

-2


In this case the best would be to have an Aplication console application (for example) to read the database, with this application You only need to schedule it in windows to run from time to time and call the API.

If you are thinking of running in cloud you could already leave for a serveless solution (as the Azure Functions)

  • rereading what the company requested (in English), matches what you suggested of creating a console application to read the bank. But I will use a package to schedule it by the application itself. Since this is a test to get into the company and I can do it at home, then it will be something simple. I really appreciate your cooperation!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.