Scheduling Web Api method to run on set intervals

后端 未结 4 2433
夕颜
夕颜 2021-02-20 00:58

In my current project there is a need to schedule a method to run at set intervals e.g. once a week, and currently this is done via a windows service creating an HttpClient and

4条回答
  •  死守一世寂寞
    2021-02-20 01:11

    There is not really a way within the Web Api to achieve what you desire. Web Apis should be stateless anyway.

    You could theoretically create a long-running task on your web server to periodically call your Api (or the method directly), the best place to put such a method would be the OnStart method within your global asax file. However, this method is run when the web server is loaded into the app-domain, and the task will be killed when the web server decides to unload your application. So if you don't call your web server at least once, your task won't be started. And if your web server is not access periodically, your task will be killed.

    Having an external reliable resource, such as your service, is still the best and safest approach.

提交回复
热议问题