How to warm up an ASP.NET MVC application on IIS 7.5?
Asked Answered
B

3

18

We would like to warm up an ASP.NET MVC application hosted on IIS 7.5 server. The warm up module that used to be available at http://forums.iis.net/t/1176740.aspx has been removed since sometime.

The application should be warmed up everytime IIS or ASP.NET worker-process restarts for any reason. During the warm up period, IIS should return some HTTP status code signifying its warm up state or its inability to serve any clients.

Would creating a executable that navigates through necessary pages in the site via HttpRequests be a good idea? The executable can be triggered from IProcessHostPreloadClient implementation. Is it possible to configure IIS so that it would only accept requests from localhost and once the executable is done, it can switch over to all clients - but that switch should not trigger an IIS restart (obviously).

Is it possible to use an Visual Studio 2010 - Web Performance Test to warm-up an application instead of creating an manual executable? Any other alternatives?

PS: The application uses Forms Authentication and uses sessions - so maintaining state cookie and other cookies is important.

UPDATE 1 - We are using .NET Framework 4.0 and Entity Framework (database first) in our application. The first time hits to EF queries are slow. The reason behind the warm up is to get these first time hits out of the way. We are already using compiled queries at most places and we have implemented pre-compiled views for EF. The size of the model and application is very large and complex. Warm up needs to walk through many pages to ensure that compiled and non-compiled EF queries get executed at-least once before any end user gets access to the application.

Binominal answered 12/9, 2011 at 11:25 Comment(2)
Related: #13251179Harijan
Try checking my solution. It won't return a special status code, but will instead wait for warmup to respond to requests.Choose
W
12

Microsoft has released a module that does exactly what you ask for. The Application Initialization Module for IIS 7.5 improves the responsiveness of Web sites by loading the Web applications before the first request arrives.

You can specify a series of Urls that IIS will preload before accepting requests from real users. I don't think you can get a true user login expereince, but maybe you can set up simulated pages that does not require login that fulfills the same warmup you ask for?

The feature I think is most compelling is that this module also enables overlapped process recycling. The following tutorial from IIS 8.0 include a step-by-step approach on how to enable overlapped process recycling.

When IIS detects that an active worker process is being recycled, IIS does not switch active traffic over to the new recycled worker process until the new worker process finishes running all application initialization Urls in the new process. This ensures that customers browsing your website don't see application initialization pages once an application is live and running.

This IIS Application Initialization module is built into IIS 8.0, but is available for download for IIS 7.5.

Weinshienk answered 20/9, 2012 at 20:52 Comment(0)
L
10

You may take a look at the following post for the Auto-Start feature built into IIS 7.5 and ASP.NET 4.0.

Lunkhead answered 12/9, 2011 at 12:4 Comment(7)
Thank you for taking an interest in this. Please do check out "UPDATE 1" in original query which gives more context to the problem. Using the method specified in ScottGu's blog may not help accomplish all the requirements we have. We need to have the site live - but not accessible to all till it is warmed up. We need to walk through our pages before allowing it to be accessed by all.Binominal
@Dhwanil Shah, the warmup happens when IIS starts running, i.e. after your server reboots and much before the first request has ever had time to hit your application.Lunkhead
Would I be able to access pages in my site from code inside an implementation of IProcessHostPreloadClient?Binominal
@Dhwanil Shah, what do you mean by access pages in my site? If it is to send HTTP requests to them, then no, you won't be able to do that inside your IProcessHostPreloadClient implementation because IIS won't allow any HTTP requests until the warup process is finished. That's the whole point of the warmup.Lunkhead
I need to access the pages in my site to warm up the EF queries used in them. I need to login, navigate, search, add-edit etc. using HTTP requests. We already have basic amount of "normal" warm up in Global.asax's application-start event.Binominal
@Dhwanil Shah, in this case you cannot use the auto-start feature. You will have to write some bot scripts that navigate your site. Of course your site will have to be online for this to work. Or if you have the possibility to put it offline for the normal users and still allow some internal users to access it you could program those scripts to access the site from internal network or something. Once you have crawled all your pages with the bot you could put the site online. I think your question is better suited to serverfault.com as it is not exactly programming related.Lunkhead
I have posted a query on serverfault.com - serverfault.com/questions/310479/…Binominal
C
6

Any application that generates a server request for the hosted resources can be used to warm up an IIS process. Exactly how many requests you need depends on what parts need warming up. Typically, warm-up is used for:

  • Starting up a worker process. For this, you only need to ask for one resource to warm up a process for the entire application.
  • Perform any static initialization, database startup, or pre-caching. Anything you do in your Global.asax file will happen when you do your first request, so if you can make all of your initialization happen then, you'll still only need to make one page request.
  • Force pre-compilation of ASP.NET pages. For this to happen you would need to hit every page. Fortunately, this is typically not much of a time cost, so you likely don't need to worry about it. If you do have individual pages that load slowly, you can warm them up separately.

The "warm-up" process here isn't anything magical. You just need force IIS to serve the URL in question. Everything you mentioned would take care of that: using a stress-test tool to query the URL, writing a custom utility to post HTTP requests, even just scripting out a tool like 'wget' or a PowerShell script to download the URLs would do it.

As far as restricting access to localhost, as far as I know, within IIS, the only way to change that requires you to restart IIS. You could always build a pre-request hook into your application and maintain the state there, and have your warm-up process query some specific URL that toggles that state to "open". But I'm not sure what you would accomplish. If, somehow, a user did try to query your site before your warm-up finished, all that would happen is your site would take a long time to respond, then they would eventually get the page they asked for. If you locked them out of the site during warm-up, they would instead get a browser network error that claimed the site was offline, which (to me) sounds much worse.

Canberra answered 12/9, 2011 at 12:4 Comment(3)
You can precompile the pages before deployment (during your build process), which has an additional benefit of finding bugs that are not detected during C#/VB.NET compilation.Airbrush
@Michael - Thank you for taking an interest in this. Please do check the "UPDATE 1" to get more context on the query.Binominal
@Jakub - We are using ASP.NET pre-compilation.Binominal

© 2022 - 2024 — McMap. All rights reserved.