We will discuss the ASP. NET processing process in three parts. Here we mainly discuss the two parts above WebApplication. This section compares the roles of IIS In Asp.net.
After learning about these processes, we can define our own WebServer. WebServer does not only support IIS, but does not support asp.
The example here is the implementation of WebHost of WebMatrix. By modifying these classes, we can implement a small function: Host assigns a ServiceManager instance to each WebApp. No copyright issues, right ?)
Part 1: WebHost
Listen for requests from the port and accept requests to form an HttpWorkerRequest
1: Create a socket port to answer
Listener.
2: Create a WebHost
Use the ApplicationHost. CreateApplicationHost (typeof (Host), virtualPath, physicalPath) static function provided by. net to create the Host space processed by Asp.net.
This Host is inherited from MarshalByRefObject and can be called across program domains. This is critical because each WebApp is assigned an AppDomain for running. Therefore, the Host must be able to create these AppDomains and call them.
3: implement the abstract class HttpWorkerRequest
. Net provides the implementation of SimpleWorkerRequest. You can simply call it directly. To be more complex, you need to rewrite more methods by yourself.
This class encapsulates all the properties and data passed down.
The unique connection point between the WebHost and each specific WebApp.
Part 2: Process HttpWorkerRequest
HttpContext and IHttpHandler are generated based on HttpWorkerRequest. This part seems to have entered several classes in. net. I don't know if it can be controlled.
1: First processing of HttpRuntime
Create a context based on HttpWorkerRequest and an IHttpHandler instance based on contxt. hanlder starts to run according to the context. Then the webpage is processed.
The System. Web. HttpRuntime. ProcessRequest (HttpWorkerRequest wr) static function is called to access the processing.
System. Web. HttpRuntime receives the HttpWorkerRequest object. Look at this function:
- PublicstaticvoidProcessRequest (HttpWorkerRequestwr)
- {
- // Ignore other details
- HttpContextcontext1=NewHttpContext(Wr, false );
- // Create context based on HttpWorkerRequest
- IHttpHandlerhandler1=HttpApplicationFactory. GetApplicationInstance (context1 );
- // Create an App instance based on context
- Handler1.ProcessRequest (context1); // run the instance. The parameter is context.
- }
2: HttpContext (HttpWorkerRequest, false)
Create HttpContext according to HttpWorkerRequest.
Just look at these two sentences.
Request = new HttpRequest (wr, this );
Response = new HttpResponse (wr, this );
Both request and response are constructed based on wr.
3: Let's see how HttpRequest is constructed.
This is the original code
- internalHttpRequest(HttpWorkerRequestwr,HttpContextcontext)
- {
- this._contentLength=-1;
- this._wr=wr;
- this._context=context;
- }
Part 3: webpage processing.
Now that IHttpHandler and HttpContext have been generated, the rest is the specific WebApp.
IHttpHandler then goes to every page. It becomes WebApplition. Not to mention it.
At this time, handler has obtained HttpContext.
Among them, What IIS does seems to be the first part of the function. If we build a host ourselves, we will mainly complete the first part.
Let's look at the definitions of these classes in WebMatrix.
1: WebMatrix. Server
This class is used to provide external operation interfaces. Inherited from MarshalByRefObject. It can be called across domains.
Main Operations: CreateHost: Create a WebHost Based on the port number, virtual directory, physical root directory, and other information), StopWebServer, and StartWebServer.
Key code: host = ApplicationHost. CreateApplicationHost (typeof (Host), this. _ virtualPath, this. _ physicalPath); the code used to create a Host.
2: WebMatrix. Host
This is the host class that creates a processing process space for each WebApp. Inherited from MarshalByRefObject
Main Operations:
OnSocketAccept {new connection; connection. ProcessOneRequest (host, this );}
After receiving the socket, call to process the request
3: WebMatrix. Connection
Connection Processing
Main Part: Call Request
- rocessOneRequest()
- {
- Requestrequest1=newRequest(this._host,this,this._serviceManager);
- request1.Process();
- }
4: WebMatrix. Request
Important. Inherited from SimpleWorkerRequest. SimpleWorkerRequest is inherited from HttpWorkerRequest. HttpWorkerRequest is the unique connection point between the host and WebApp, and is the only entry parameter of WebApp.
This class mainly overrides the Process method and processes webapps by calling the HttpRuntime. ProcessRequest (this) code.
Now, let's complete a small function of my own.
1: first obtain the source code of the Matrix WebServer. Use the Reflector tool.
2: Modify the Request class, that is, the class inherited from SimpleWorkerRequest, and add an attribute: ServiceManager
3: Modify the Host and Server so that ServiceManager can be passed to the Request.
4: Use: this can be used in each WebApp.
IServiceProvider p = (IServiceProvider) HttpContext. Current;
Request wr = (Request) p. GetService (typeof (HttpWorkerRequest ));
Object o = wr. ServiceManager;
For example, this is in the page_load of a webpage.
Notes
1: Physical root directory
Just like wwwroot, make the root directory of the entire site. For example, c: \ maxsoft. site
2: virtual directory
Relative to the location after the root directory. For example, the virtual directory of c: \ maxsoft. site \ myTest is/myTest.
3: Port Number
It can be customized as long as it does not conflict with the system. For example, 6066
4: Access Method
Target machine: Port Number/virtual directory/file name. For example, http: // maxpc2/myTest/webform1.aspx
5. Installation
Be sure to include this WebServer program in the bin folder of the physical root directory. For example, copy "Maxplatform. UI. Web. WebHost. dll" to the c: \ maxsoft. site \ bin \ directory. This file is the Assembly with the WebHost class compiled. The above describes the ASP. NET processing process.
- XML data displayed on the ASP. NET page
- What is iframe asp. NET?
- ASP. NET lifecycle display
- Solve the Problem of ASP. net ajax script errors
- ASP. NET Applications