Inside ASP. NET 2.0-instant compilation system

Source: Internet
Author: User

 

Evolution of compilation systems from ASP. NET 1.1 to 2.0

I wrote "in-depth analysis of ASP. NET Component Design, I used to discuss ASP in depth. NET 1.1 instant compilation model, this chapter in Figure 1 as the beginning, step by step will be invisible in the design concept spread in front of the publisher, to today, Asp. net is about to enter 2.0. This Real-Time compilation model has undergone considerable changes. Figure 2 shows an overview of the real-time compilation model in comparison with 1.1 and 2.0. We can find that, 2.0 of real-time compilation models are much more complicated. Figure 1

 

Figure 2

 

 

At 1.1, when a visitor requests a file, isapiruntime (which is required by IIS to process the object) will follow the file type to evoke the appropriate HTTP handler. aspx is called pagehandlerfactory, which is also the entry to the real-time compilation system. This process remains unchanged in 2.0, but the subsequent actions are completely changed. at 1.1, pagehandlerfactory will use pageparser for interpretation. the aspx file is then handed over to pagecompiler to generate the compilation file. At 2.0, the same action is done by buildmanager, which calls the appropriate buildprovider to process the required file, and finally submits it to the appropriate compiler to generate the compilation file. I wonder if the author sees the Implied Meaning of the above paragraph, yes! Buildmanager has the ability to use different buildproviders according to different file names, which means that designers may have the ability to write custom buildprovider to compile and compile the process in real time. Users can see the figure 3 in the new items option of visual web developer. Figure 3

 

The most notable among them is that ASP. NET 2.0 allows users to write generic handler, that is, the custom HTTP handler program file in 1.1. This Wizard will generate the code of program 1.

Program 1

 

<% @ Webhandler Language = "C #" class = "handler" %>
Using system. Web;
Public class handler: ihttphandler {

Public void processrequest (httpcontext context ){
Context. response. contenttype = "text/plain ";
Context. response. Write ("Hello World ");

}
Public bool isreusable {
Get {
Return false;
}
}
}

Have you seen a strange program code between ASP. NET script and general program? The result shown in Figure 4 is displayed after the archive operation.

Figure 4

 

The problem arises. In the past, when writing a custom HTTP handler, the designer must compile the code in advance and place it under the website's contents to make the handler work normally, but this compilation action has not been executed yet? Who compiled this file for us and how did it work? The answer is the same as that in 1.1, that is, simplehandlerfactory, but the subsequent actions are different. In the past, simplehandlerfactory only loads the corresponding assembly to start HTTP handler. In 2.0, this action was replaced with buildmanager, she will look. the build provider corresponding to ashx, that is, webhandlerbuildprovider, to run the instant compilation model. In the above discussion, most of handler factory's work has been delegated to buildmanager, and the purpose is to provide a more powerful instant compilation model, not just compilation. aspx ,. ascx can also compile a variety of files, such. masterpage is also a member of this model, which brings a great advantage that is hard to imagine. Designers Will be able to customize script files in the future, as long as there is a need, designers can define a script language by themselves, and then provide the corresponding buildprovider object. buildmanager can easily complete real-time compilation for you. Besides, buildmanager supports the pre-compilation model, that is to say, after the designer provides the script file and buildprovider, he can enjoy two models: instant compilation and pre-compilation. To give a more practical example, if a designer wants to provide some simple script language for the user's application, the designer only needs to provide a myscriptbuildprovider and match it with a specific file name, you can use codedom to generate real code, and the next action buildmanager will help you with it.

Reloaded! Page compiler-time

Since 2.0 has changed the instant compilation model, let's get to know how the compilation system is.

In 1.1, the most important thing in the instant compilation system is the pageparser object, which is written into. aspx.

File, which is interpreted as a group of control builder objects and handed over to the pagecompiler object to generate the original code.

Compilation, this process remains unchanged in 2.0. The difference is that pageparser in 2.0 is not

Pagehandlerfactory called. Figure 5 shows an overview of the web page compilation period in 2.0.

Figure 5: When buildmanager receives the compilation command, it first compiles several external files in the project, these archives are resource, Web reference, code, profile, and global. asax, resource refers to the resource file. Web reference is usually the file used to reference web services. Code is the program file under the Code object, and the profile is located in the web. the profile section definition in config, and global asax is the global. asax file. Next is the main scene of this Section, compiling web files! This action will compile the. aspx file on the website or other files with the corresponding buildprovider object, such as. ashx and. masterpage. Return to the page compilation cycle ,. the buildprovider corresponding to aspx is a pagebuilderprovider object, which is interpreted using pageparser. as PX, use pagecodedomtreegenerator to generate the original code, and finally compile it by appropriate compiler.

Manager, provide R, generator

In the previous section, buildmanager has an inseparable relationship with buildprovider and its codegenerator. Figure 6 shows a buildprovider object provided in 2.0. The author can find many familiar object names in it, they correspond to the current.. net.

Figure 6

 

 

Interestingly, page theme also has a pagethemebuilderprovider. What does this mean? I thought theme was just a simple text file. When a theme is applied to a page, the definition of theme is only applied to the control by the operator in the text file, but the result is not true, from the appearance of pagethemebuilderprovider, theme is a compiled file. pagethemebuilderprovider compiles all theme files, that is. skin, in fact, all internal control definitions are compiled into control entities. When the page needs to apply a theme to the control, it just copies the control attributes, without interpretation, the speed is naturally much faster. Basically, all buildprovider objects that can be compiled provide two objects: Parser for file interpretation and codedomtreegenerator, it is used to convert the control builder object group into compiled original code. In pagetheme, 7 is shown. Figure 7

 

Of course, this does not mean that every buildprovider has to provide these things. The contract layer stops when it reaches buildprovider. As long as the buildprovider can return an object, buildmanager does not care about how it is achieved internally.

Pre-compiled System

So far, we have been turning on the instant compilation system, not talking about another system, that is, the pre-compilation system. In fact, this system is just a type of presentation of the instant compilation system, when buildmanager is started, the system first determines whether the required object has a property. compiled files are regarded as pre-compiled files and loaded. the Assembly defined in the compiled file will wait! The real. aspx file after pre-compilation does not exist. How does buildmanager interact with buildproviders after completing the next action? Oh, no! In the case of pre-compilation, buildmanager does not use buildprovider at all, which is the same as the second action of the real-time compilation system. After the Real-Time compilation system is complete, the result is saved to the saved object, the pre-compiled system skips the first action of the pre-compiled system. This means that, custom buildprovider can enjoy the advantages of the pre-compiled system without special actions.

 

Custom Build provider

As ASP. NET 2.0 is still in Beta, there is little information about build provider, but I still find the description of Program 2 in the file. Program 2

 

<Configuration>
<System. Web>
<Compilation>
.......
<Buildproviders>
<Buildprovider extension = ". mafx" type = "buildprovidertype, buildproviderassembly"/>
</Buildproviders>
</Compilation>
</System. Web>
</Configuration>

 

The section in bold is the place where the custom build provider is defined, which can be proved in ASP. in NET 2.0, designers are allowed to write build providers. However, apart from this file, I can no longer find more in-depth information. Unfortunately, this file has an error, the buildprovider definition is unacceptable, and the actual syntax should be as follows: Program 3. Program 3

 

<Compilation DEBUG = "true">
<Buildproviders>
<Add extension = ". PPP"
Type = "testbuildprovider. mycsharpbuilder, testbuildprovider"
Appliesto = "code"/>
</Buildproviders>
</Compilation>

 

Extension defines the sub-file name corresponding to buildprovider, type refers to the Assembly and type of buildprovider, and the final appliesto represents the mode used, there are four options, first, code indicates the program file, second, resource indicates the resource file, and third, Web indicates the webpage file (. aspx ,. ascx ...), The fourth is all, which represents any type of archives. To write buildprovider, the compiler must first prepare Visual Studio 2005 Beta or Visual C # Express. These tools provide the class library wizard. Otherwise, the command line method must be used to compile the example, program 4 is our first buildprovider example. Program 4

 

# Region using directives
Using system;
Using system. Collections. Generic;
Using system. text;
Using system. IO;
Using system. codedom;
Using system. codedom. compiler;
Using system. Web. compilation;
# Endregion

Namespace testbuildprovider
{
Public class mycsharpbuilder: buildprovider
{

Public override void generatecode (assemblybuilder)
{
Textreader reader = base. openreader ();
String scriptstring = reader. Readline ();
Codecompileunit unit = new codecompileunit ();
Unit. namespaces. Add (New codenamespace ("test "));
Codetypedeclaration class1 = new codetypedeclaration ("helloclass ");
Class1.isclass = true;
Codemembermethod Method1 = new codemembermethod ();
Method1.name = "sayhello ";
Method1.returntype = new codetypereference ("system. String ");
Method1.statements. Add (New codemethodreturnstatement (New codeprimitiveexpression (scriptstring )));
Method1.attributes = memberattributes. Public;
Class1.members. Add (Method1 );
Unit. namespaces [0]. types. Add (class1 );
Assemblybuilder. addcodecompileunit (this, Unit );
}
}
}

 

Let me explain this example a little. A helloclass class is generated using codedom in the program. Add a method: sayhello to define a string-type return value for it, note that this value is returned from the textreader returned by openreader. openreader will use textreader to open the currently processed files. In this example, It is class1.ppp (details later ). After compilation, copy it to the bin category in the website category. If your website does not have a bin category, create one on your own. Then modify web. config to add the definition of program 5. Program 5

 

<Compilation DEBUG = "true">
<Buildproviders>
<Add extension = ". PPP" type = "testbuildprovider. mycsharpbuilder, testbuildprovider"
Appliesto = "code"/>
</Buildproviders>
</Compilation>

 

Create a file class1.ppp, for example, program 6. Program 6

 

Hello I am buildprovider, this message is define in class1.ppp.

 

Put this file in the code category under the website category. If there is no code category, create one. Now let's try whether buildprovdier works properly. Put a button in default. aspx and write its event function, such as program 7. Program 7

 

Type findtype ()
{
Assembly [] assems = appdomain. currentdomain. getassemblies ();
Foreach (Assembly assem in assems ){
Type T = assem. GetType ("test. helloclass ");
If (T! = NULL) return T;
}
Return NULL;
}

Void button2_click (Object sender, eventargs E)
{
Type T = findtype ();
If (T! = NULL)
{
Object OBJ = activator. createinstance (t );
String S = (string) obj. getType (). invokemember ("sayhello", bindingflags. invokemethod | bindingflags. instance | bindingflags. public, null, OBJ, new object [] {});
Button2.text = s;
}
}

 

The findtype function is written to find the helloclass type. During website execution, although the assemblys generated by buildprovider is loaded, however, the actual name of the Assembly generated by class1.ppp cannot be known here, And helloclass cannot be obtained. Therefore, use findtype to search for all assemblys to obtain helloclass. Assembly cannot be determined, and of course it cannot be used to create objects and call methods in a general way. Therefore, only reflection can be used. Here, reflection is used to create helloclass object entities, then, call its sayhello to retrieve the text in class1.ppp.

Another compilation subsystem: expression Builder

Build provider is a pretty good design. Designers can write custom providers to extend ASP. net compilation system, but sometimes designers only need a simple dynamic resolution system, rather than a file-based compilation action, as shown in program 8. Program 8

 

Connectionstring = "<% $ connectionstrings: appconnectionstring1 %>"

 

This is a simple design built in ASP. NET. The strings after <% $ will be interpreted as code 9 during compilation.

Program 9

 

Source1.connectionstring = connectionstringsexpressionbuilder. getconnectionstring ("appconnectionstring1 ");

 

With this design, the designer can point the value in the configuration file to a certain attribute to achieve the purpose of changing the behavior of an application in an external file, or reduce the number of times that the program is recompiled, how can this be achieved? What should we do if we want to customize this function? The answer already appears in program 9, that is, the expressionbuilder object. ASP. NET 2.0 allows designers to write custom expressionbuilder objects such as build providers. program 10 is a simple example. Program 10

 

# Region using directives
Using system;
Using system. Collections. Generic;
Using system. text;
Using system. Web. compilation;

Using system. reflection;
Using system. componentmodel;
Using system. codedom;
# Endregion

Namespace testexpressionbuilder
{
Public class myexpressionbuilder: expressionbuilder {
Public override system. codedom. codeexpression getcodeexpression (system. Web. UI. boundpropertyentry entry, object parseddata, expressionbuildercontext context)
{
Return new codeprimitiveexpression (entry. expression );
}

Public myexpressionbuilder ()
{
}
}
}

 

The program contains the getcodeexpression function, which is called during the interpretation period. At this time, a codeexpression object must be returned, and the interpreter generates code similar to program 9. To apply this expressionbuilder, Web. config must contain the setting of Program 11. Program 11

 

<Compilation DEBUG = "true">
<Expressionbuilders>
<Add expressionprefix = "myexpression"
Type = "testexpressionbuilder. myexpressionbuilder, testexpressionbuilder"/>
</Expressionbuilders>
</Compilation>

 

The expressionprefix attribute represents the verification code at the beginning of the expression that expressionbuilder can parse. Only the expressions that match this string are handed over to myexpressionbuilder for processing. program 12 is a test code. Program 12

 

<Asp: button id = "button2" runat = "server"
TEXT = "<% $ myexpression: I am expxression builder %>"/>

 

However, it is a pity that the current vWD does not seem to recognize the custom expressionbuilder, so it is impossible

 

The expected results are displayed.

Postscript

. The author is reminded that Net Framework 2.0 is still in beta stage, which also indicates that the technologies currently being discussed are not fixed. Although the build provider and expression builder technologies bring unlimited imagination to designers, however, no one except Microsoft can determine whether the final version will still enable these features for designers.

 

Related Article

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.