Why is IIS slow on first access, but faster afterwards?

asp.net, build, iis

Solution

When websites are deployed to IIS, the assemblies that make up the site still need to be natively compiled. On first access, the CLR 'interprets' the MSIL and converts it into a binary version specific to the CPU - e.g. x86, x64, Itanium etc.

This also occurs with .EXE's and any outputs from the .NET compiler.

To address this issue with websites taking a while to start up, you can configure pre-compilation to be turned on. See this Microsoft article for more information on the details.

From your comment on

And, from the URL being entered in the browser, how does the .NET compiler know that it needs to convert the MSIL to binary?

IIS stores a list of websites it maintains, and knows what applications service the requests received at those endpoints. For applications written in .NET, IIS will pass the request information to an ASP.NET pipeline, which will take the information provided by IIS and place the information into various .NET objects like `HttpContext`, `Session` etc.

From there, the ASP.NET pipeline works out which part of the application responds to that request and executes the code for it.

Problem

After building my asp.net solutions, and then run it for the first time in the browser, the time it takes to load increases. Then running it thereafter the time has decreased until the next build. Can someone explain why this happens?

Original source