When does PreApplicationStartMethod actually get t

2019-01-12 02:42发布

问题:

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?

回答1:

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.



回答2:

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.