Notas del Terrible
Terrible Bore

ASP.NET pre-compilation

January 21, 2010 22:13 by terR0Q

Any ASP.NET page may be built from several files. They can be compressed up to one, but that’s not handy, and in Visual Studio everything is always split up in 3 files. Anyway, they keep program logic (event handlers mostly) and server mark-up (common html mark-up plus declaration of elements controlled by server).

When client requests some page for the first time declarative mark-up and imperative part (well, code) are compiled by ASP.NET. Although all .cs files are compiled into one dll file for the whole project by VS, separate dll file is created for each page (more info can be found in many good books and articles). That’s a good model, but first request to a page always takes some more time than following requests: time is taken to compile and save new file in ASP.NET cache. But that can be a problem for large projects.

This problem can be solved by pre-compilation of the whole site. Most simple solution for that task, from my point of view, doesn’t need any VS add-ins or any shamanism with publishing or build events. Just 3 steps:

  1. Publish web-site into target directory
  2. Execute following command:
    aspnet_compiler. exe -m /LM/W3SVC/1/Root
  3. Wait until compilation finishes.

There are two more things to say. First of all: /LM/W3SVC/1/Root is a web-node address in IIS hierarchy where web-site is deployed. This value can be used for any ‘default’ web-site, but when there are several web-nodes on IIS, value ‘1’ in this string is changed by some other. To find correct address just a simple utility is needed — IIS Metabase Explorer. It will show full IIS web-nodes hierarchy that is pretty easy to understand ‘cause readable nodes’ names are saved here as well. When node is found you need to take MD_APP_ROOT value — that’s the address to provide for aspnet_compiler.

Second thing: pre-compilation should not be started in a project working folder. If you have active project with several developers in collaboration that includes source control (I hope to never see such one without SC...), than after short period of time its directory will be full of ‘dead’ files. When someone deletes file from project his VS usually erases it from drive. But when other developers will get change list, most common result will be that VS will just remove file from solution leaving it physically in place. Now, when aspnet_compiler will start working it will crawl over all ASP.NET files in directory. This will result in error and interrupted compilation when some file will address a class that exists in project dll no more (VS just has nothing to compile for these files).

Well, the last thing to do is to write script so that to automate publishing and compilation process. But that’s just a trivial local task.


Comments

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading