ASP. NET Dynamic Compilation

Source: Internet
Author: User

1. dynamic compilation process

We will first introduce ASP. NET Dynamic compilation of the general execution process: When ASP. when NET receives a request based on a page, it first checks whether the Page and related Source code have been compiled. If not, it compiles the page. If it has already been compiled, the Page object is directly generated using the loaded Assembly.

Note the following points:

1). ASP. NET Dynamic compilation is on-demand. ASP. NET only compiles aspx and code related to the current Request.

2 ). ASP. NET Dynamic compilation is based on a directory, that is, ASP. NET will compile all the files to be compiled in the directory where the requested page is located, and generate an Assembly.

3) In addition to the compiled Assembly, dynamic compilation also generates a series of auxiliary files.

4) Modifications to the relevant files may cause re-compilation, but the modifications do not work for the current Request. That is to say, if you modify An aspx, the modified Request will be re-compiled, but the previously compiled Assembly will still be used for the previous Request.

5). The compiled Files are stored in a Temporary Directory. The Directory address is Windows Directory \ Microsoft. NET \ Framework \ v2.0.50727 \ Temporary ASP. NET Files. The specific directory structure is shown in:

In Temporary ASP.. NET Files. ASPNETDeployment is the Virtual Directory name in IIS. the names of the following two directories are composed of Hash values. Therefore, the files generated by compilation are stored in the c6f16246 Directory. You can obtain this directory through HttpRuntime. CodegenDir.

Directory \ Microsoft. NET \ Framework \ v2.0.50727 \ Temporary ASP. NET Files is only a default Temporary Directory. You can set the Temporary Directory in the compilation section of web config.

 
 
  1. <compilation tempDirectory="d:\MyTempFiles" /> 

2. Sample

Now I use a Sample to show how ASP. NET performs dynamic compilation.

In this Sample, I created a Website and created two pages under the root directory: Default and Default2.

Two Web pages: Page1 and Page2 are created under the two subdirectories Part I and Part II respectively. A Utility static class is created in the App_Code directory. The following is its definition:

 
 
  1. publicstaticclassUtility  
  2. {  
  3. publicstaticstringReflectAllAssmebly()  
  4. {  
  5. StringBuilderrefllectionResult=newStringBuilder();  
  6.  
  7. foreach(AssemblyassemblyinAppDomain.CurrentDomain.GetAssemblies())  
  8. {  
  9. if(!assembly.FullName.Contains("App_Web"))  
  10. {  
  11. continue;  
  12. }  
  13.  
  14. refllectionResult.Append(assembly.FullName+"<br/>");  
  15. Type[]allType=assembly.GetTypes();  
  16. foreach(TypetypeInfoinallType)  
  17. {  
  18. refllectionResult.Append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+typeInfo.Name+"<br/>");  
  19. }  
  20. }  
  21.  
  22. returnrefllectionResult.ToString();  
  23. }  

The content is very simple. Reflection is performed on all the related assemblies currently loaded. The Fullname of these assemblies is prefixed with App_Web) to list all types. This ReflectAllAssmebly will be called in the Page_Load events of five Web pageDefault pages and two Page1 & Page2 teams.

 
 
  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3. this.Response.Write(Utility.ReflectAllAssmebly());  

Default is to list all links corresponding to 4 pages so that we can access them. When we compile again, enter the corresponding URL in IE to access the Default Page. (Html of other pages does not have real content. It is an empty Page .)

Through the above display, we can see that there is now an Assembly: App_Web_wh7-uda5. This Asssembly defines five types. _ Default and default_aspx correspond to the Default Page, and Default2 and default2_aspxDefault2 pages respectively. FastObjectFactory_app_web_wh7_uda5 is an important Type. I will introduce it in detail later. As we have mentioned above, dynamic Compiling is based on demand. Now we access the Default Page. Because of this first visit to this Website, all the required Source Code, including aspx, code behind must be compiled. In this Sample, although we have not accessed the Default2 page, we have said that ASP. NET Dynamic compilation is based on directories. Because both Default Page and Default2 Page are directly placed under the root directory, ASP. NET will compile all the files in the root directory into an Assembly. Since Page1 and Page2 are located under the sub-directories Part I and Part II, they are not involved in compilation. Unless we download and Request it.

Now let's access Page1 and Page2 under Part I to see what the results will be. We will find that the output of the two requests is the same:


Through the above output, we found that there is one more Assembly loaded in the current AppDomain: App_Web_n1mhegpg. By naming the Type defined in the Assembly, we can guess that the Assembly is produced by compiling the Part I directory. The compiled Type names of Page1 and Page2 are changed to part_ I _page?aspx & Page1 and part_ I _page2_aspx & Page2. In addition, we can see that there is still a FastObjectFactory Type in the Assembly: FastObjectFactory_app_web_n1mhegpg. Here, I need to note that the name suffixes are obtained through the Hash algorithm.

With the above theoretical and experimental results, I think at this time, you must have thought about what the output results will look like if I access Page1 and Page2 of Part II.


If at this time, you can view the temporary Directory \ Microsoft. NET \ Framework \ v2.0.50727 \ Temporary ASP.. NET Files.

  1. Microsoft released multiple function updates for ASP. net mvc 2 preview Edition
  2. ASP. NET Server custom control security guidelines
  3. Analysis of ASP. NET programming standards and Their encoding specifications
  4. Introduction to ASP. NET sessions
  5. ASP. NET programming tool ASP. NET Web Matrix

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.