19

When using the WebActivator PreApplicationStart method, what actually triggers the methods bound to this to be run? When IIS7 has started the App Pool? When the first request is made to the webserver? Something else? If you have the answer, could you please also provide a reference to where you got this information?

Does any of this change in IIS 7.5?

Eli
  • 17,397
  • 4
  • 36
  • 49

2 Answers2

27

WebActivator PreApplicationStart actually relies on ASP.NET PreApplicationStartMethodAttribute (see this link to see how web activator works).

PreApplicationStartMethodAttribute works when ASP.NET runtime starts up the application and the code runs early in the pipeline even before app_start event gets fired. So to answer your question, trigger would happen when first request is made to the web server (which will in turn kicks in application start up).

Note that trigger is related to ASP.NET app start and not with app pool. Your app pool might be running because of some other application (can be non ASP.NET app) but when first request comes for the ASP.NET app, this trigger would happen (for particular app) because application gets started.

If you are using auto-start feature then IIS will re-start your application on your app pool recycle and thus PreApplicationStart will get triggered.

Elliott Beach
  • 10,459
  • 9
  • 28
  • 41
VinayC
  • 47,395
  • 5
  • 59
  • 72
  • The article you gave from ScottGu was great. I am curious to know what you think about this `IProcessHostPreloadClient` interface. Since it seems that you can have your own caching logic before the app even receives its first request, do you think this is a good alternative as opposed to using `PreApplicationStartMethod` for running code before the requests are received? – Eli May 11 '11 at 13:01
  • @Eli, the exact purpose of IProcessHostPreloadClient is to add warm-up logic for application (the code that traditionally runs in app_start event or PreApplicationStartMethod. The only issue is that it needs IIS 7.5 and there are still few prod environments based on Win 2003/2008 and dev environments on XP. – VinayC May 12 '11 at 04:02
  • @VinayC, the auto-start feature will NOT trigger Application_Start, are you sure it triggers the PreApplicationStart event? – Tormod Hystad Jan 04 '12 at 14:25
  • @TormodHystadm, your are correct! Auto-start feature will start app-pool and call configured start-up code but will not trigger actual ASP.NET application (that is responsible for `PreApplicationStart`). You have to either move your start-up code in auto-start code or make HTTP request to your app in auto-start code. – VinayC Jan 05 '12 at 04:57
  • 1
    In Azure `WebActivator.PreApplicationStartMethod` start method triggers multiple times.What could be the possible issue? – ManirajSS Jan 28 '16 at 08:15
  • Any replacement for this feature in ASP.NET Core projects? – Shimmy Weitzhandler Feb 13 '17 at 09:54
  • This link no longer works. http://ilearnable.net/2010/11/22/webactivator-preapplicationstartmethod/ – Praveen Reddy Feb 19 '17 at 00:41
  • @CodingDawg, perhaps this may help: http://gentlelogic.blogspot.in/2014/07/how-does-webactivator-works.html – VinayC Mar 30 '17 at 09:15
8

A small supplement to @VinayC's answer: if you add a breakpoint in your PreApplicationStartMehod and debug your web application, you may see it being invoked on every request. I easily confirmed that this isn't the usual behavior by writing to a log file in my PreApplicationStartMethod. When not attached to the debugger, this method does not run on every request.

mbaynton
  • 411
  • 4
  • 4
  • In Azure `WebActivator.PreApplicationStartMethod` start method triggers multiple times.What could be the possible issue? – ManirajSS Jan 28 '16 at 08:16
  • @ManirajSS You can have multiple application instances. http://stackoverflow.com/a/6957462/242520 – ta.speot.is Feb 21 '16 at 04:46