3

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.

0x49D1
  • 8,505
  • 11
  • 76
  • 127

2 Answers2

3

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.

thiagoleite
  • 503
  • 1
  • 3
  • 11
  • Nice article, thank you. And i know that my case is bad practice, tho, as i already mentioned, the code was written before me, so i have to make it run without user requests to website. – 0x49D1 Dec 12 '11 at 12:50
  • You might want to implement the code he uses to run the background task, also just make a service that keeps making requests to your site, so the app pool won't stop, and no user will be necessary to start the process. Also can't you setup Quartz.net as a service? – thiagoleite Dec 12 '11 at 12:54
  • i think about replacing that website with WCF service. Tho as i know webservice will be request based too..Is there an event that will be invoked on webservice first start? – 0x49D1 Dec 12 '11 at 13:03
  • ..http://stackoverflow.com/questions/739268/wcf-application-start-event , hope static constructor will help me too =) – 0x49D1 Dec 12 '11 at 13:04
  • WebService will be no different than regular webforms or mvc asp.net app. You are going to have the same problems... consider installing quartz.net as service, or ask your hosting to setup a scheduled task for you in windows. I can't think of any other solution for you. Also creating a kind of "keep-alive" service not an option for you? Definitely not ideal but its something. – thiagoleite Dec 12 '11 at 13:07
  • so you suggest to write a windows service, that will do scheduled request to the website, and run it on the same machine, where the website is running? – 0x49D1 Dec 12 '11 at 13:12
  • 1
    No, i'm sugesting you to install [Quartz.NET](http://quartznet.sourceforge.net/) as a service. This way you will be able to setup schedule tasks(just like Scheduled Tasks in windows), but remotely, using XML for example. Check out their website for more information. Check out their [tutorial](http://quartznet.sourceforge.net/tutorial/index.html) its easy. – thiagoleite Dec 12 '11 at 13:22
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/5774/discussion-between-thiagoleite-and-0x49d1) – thiagoleite Dec 12 '11 at 13:24
  • Ok, will check that..Seems like a very powerful library. – 0x49D1 Dec 12 '11 at 13:24
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
Muthu
  • 2,675
  • 4
  • 28
  • 34