We are aware that the complition process of Asp.Net 1.x Web page has two phases.
1. Code Behind files and other supporting classes compiled into an assembly.
2. And Individual ASPX files are compiled at runtime in to temporary assemblies. This happens during the page request has been performed.
So firstime when the user requests a page its slow. I have seen suggested approach like writing a precompile.aspx page which goes and fires requests [ (HttpWebResponse)myHttpWebRequest.GetResponse(); ] to individual pages available in the site. Example code looks like the following.
///
/// PreCompile all ASPX pages for the current application.
///
public void PreCompileAspx()
{
string rootDirectory = Request.PhysicalApplicationPath;
Response.Write("Server path is >> " + rootDirectory);
string url = Request.RawUrl;
Response.Write("
Virtual path is >> " + url);
//Get the virtual root directory.
int index = url.LastIndexOf('/');
string virtualDirectory = url.Substring(0,index+1);
Response.Write("
Virtual directory is >> " + virtualDirectory + "
");
ArrayList arrayList = GenerateUriList(rootDirectory,"http://localhost" + virtualDirectory);
foreach(string uri in arrayList)
{
FireRequest(uri);
Response.Write("
PreCompiled ASPX resource >>> " + uri);
}
}
///
/// Loops thru the files on the server hard-disk and creates URI's to fire requests to.
///
/// The server local directory path
/// The virtual ROOT path
/// ArrayList of all URL's of ASPX files in that application root folder
public static ArrayList GenerateUriList(string directory, string rootUri)
{
ArrayList arrayList = GetAllFiles(directory,"*.aspx",true);
ArrayList finalList = new ArrayList(arrayList.Count);
foreach(string str in arrayList)
{
string uri = str.Replace(directory,rootUri);
uri = uri.Replace(@"\","/");
uri = uri + "?PreCompile=true";
finalList.Add(uri);
}
return finalList;
}
///
/// Simulate a HTTP request. Make the request to 'localhost' as this file is present on the server.
///
/// The URL to make the request to.
public void FireRequest(string uri)
{
HttpWebRequest myHttpWebRequest = null;
HttpWebResponse myHttpWebResponse = null;
try
{
myHttpWebRequest = (HttpWebRequest)WebRequest.Create(uri);
myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
}
catch(Exception ex)
{
Console.WriteLine("Request for uri faided>>" + ex);
}
finally
{
try{myHttpWebResponse.Close();}
catch(Exception ignored){}
}
}
///
/// Gets a list of all files in a directory. Also recursively..:)
///
/// The name/path of the directory
/// The filter on files returned. For e.g. only text files, pass "*.txt".
/// Pass null if no specific filter is required.
/// Whether to search for sub-directories recursively or not.
///
public static ArrayList GetAllFiles(string directory,string filter,bool getFilesInSubDirs)
{
ArrayList totalFilesList = new ArrayList(10);
string[] files = null;
if(filter == null)
{
files = Directory.GetFiles(directory);
}
else
{
files = Directory.GetFiles(directory,filter);
}
totalFilesList.AddRange(files);//add all files in that current folder.
if(getFilesInSubDirs)
{
//Check if the current directory has sub-directories
string[] subDirs = Directory.GetDirectories(directory);
//if yes, then call recursive functions..
if(subDirs.Length > 0)
{
//now look for all files in current folder's sub-dir's.
foreach(string subDir in subDirs)
{
ArrayList tempArrayList = GetAllFiles(subDir,filter,getFilesInSubDirs);
totalFilesList.AddRange(tempArrayList);
}
}
}
return totalFilesList;
}
But there is another option we can do batch compilation and can be configured in web.config by tweaking the tag. Batch compilation reduces load time of the first request of the Web page, the other good thing is all aspx files are compiled into one single temporary assembly.
In asp.net 2.0 batch compilation can be performed with a single URL request. In both Asp.Net 1.x and 2.0 it still takes longer cycle time on startup but removes delay on the first page request. The web.config tweak works with asp.net 2.0 as well. Another good thing is ASPX page errors will be detected during batch compilation. Please note that if a file fails its thrown out of the batch.
Here are the options to tweak web.config to do batch compilation:
<compilation
batch="true|false"
batchTimeout="number of seconds"
maxBatchSize="maximum number of pages per batched compilation"
maxBatchGeneratedFileSize="maximum combined size (in KB) of the
generated source file per batched compilation"
