1

IN my asp.net MVC4 project I have few .cs files under the app_start folder that needs to be initialized or perform per-authorization tasks etc. When you access the website they take quite a lot of time to perform all these tasks on application start for first time. Is it possible to run all files under app_start folder automatically while deployment. So that when someone access the website for the first time it wont take that much time.

sam
  • 4,594
  • 12
  • 61
  • 111

1 Answers1

2

Take a look at auto-starting your application

Add this to your applicationHost.config

<sites>    
     <site name="MySite" id="1">    
          <application path="/" 
                       serviceAutoStartEnabled="true" 
                       serviceAutoStartProvider="PreWarmMyCache" />    
     </site>    
</sites>
<serviceAutoStartProviders>
     <add name="PreWarmMyCache" type="PreWarmCache, MyAssembly" />
</serviceAutoStartProviders>

and the following to preload your work

public class PreWarmCache : System.Web.Hosting.IProcessHostPreloadClient {

    public void Preload(string[] parameters) {

        // Perform initialization and cache loading logic here...

    }

}
dove
  • 20,469
  • 14
  • 82
  • 108
  • Thank you Dave, Can you please confirm do I have to add this in web.config as you are saying or in applicationHost.config file as explained in your referred link. – sam Nov 28 '12 at 09:15
  • Thank you so much. Can you please also guide Do I have to replace my public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() with the public class PreWarmCache : System.Web.Hosting.IProcessHostPreloadClient { – sam Nov 28 '12 at 09:25
  • No it does not replace. It's a separate class and it can put it in the root of your web app or wherever you want so long as the serviceAutoStartProviders configuration namespace matches. – dove Nov 28 '12 at 09:32
  • Thanks dave, Performed all steps but getting Service Unavailable HTTP Error 503. The service is unavailable. Any idea what I am doing wrong – sam Nov 28 '12 at 11:31
  • something is very wrong http://stackoverflow.com/questions/5420869/iis-7-service-unavailable-503-error double check all config settings and steps in that article. try applying one change at a time until it breaks. – dove Nov 28 '12 at 11:42