Run job on ASP.NET website service start in IIS

2019-07-20 01:57发布

问题:

I have some method that is invoked on Application_Start. And it starts on first page request by any user.

But i have another application, dependent on that task that fires during Application_Start.

So that if IIS/website is restarted- another application cant get some data from website, until someone requests page on the running website.

Is there a way to run some task on website start; without any interaction from user side, just to start website in IIS and do the job i want?

PS more details on implementation architecture:
Website starts an instance of messaging service and starts sending messages through that service(that is located on another server). I know that it would have been better to create wcf or windows service for this kind of things, but someone wrote this website before i was included in this project. So clients(that receive messages from messaging server) do not know anything about website. Here website is just administrative panel to turn on/off some messaging features.

My Solution
Created windows service for my task. Works well, under local system account has no problems with writing to windows event logs.

回答1:

Check out this "The Dangers of Implementing Recurring Background Tasks In ASP.NET"

Also you should consider installing Quartz.NET as a service, so that you can setup scheduled tasks remotely.



回答2:

You can try this.

  • Put an entry in your code into event log
  • Use the windows task scheduler to create a task
  • Create a trigger as "Begin the task On an event"
  • Create the action with the job you want