If you want to have a better reading experience, go to my github blog, http://lcomplete.github.io/blog/2013/07/16/use-csharp-write-aspnet-web-server /.
Have you ever wanted to run the ASP. NET program and do not want to install IIS or Visual Studio? I think if you often write ASP. NET programs, you may encounter this situation more or less. In addition to IIS and VS, how can we run ASP. NET programs? How can we write a Web server that supports ASP. NET? NO. If you just want to find such a tool, it is completely unnecessary. We know that using VS can run ASP. NET program, then we can find out the program called by VS and copy it to the environment without VS and IIS to run ASP. NET Program. If VS is installed, you can find it in the C: \ Program Files \ Common Files \ Microsoft Shared \ DevServer \ directory. The usage of this Program is as follows.
WebDev.WebServer.EXE /port: /path: /vpath:
How is it? Well, it's easy to solve the problems mentioned at the beginning of the article. Of course, this is not the focus of this Article. If you are not satisfied with the usage, you can continue to read it. Next, we will use C # To compile a program that supports ASP. NET Web server to see how it works.
C # has many rich class libraries. Using Different class libraries, we can write a Web server at different abstract levels, such as in System. the Net namespace provides an HttpListener class. With this class, we can easily create a simple Web server, but this class hides many implementation details, to avoid knowing why, we will use the Socket class at the bottom of the network framework to write this program.
Prerequisites
Before writing this program, let's first understand some basic knowledge. To compile a Web Server, you must understand the HTTP protocol, which is the foundation of the World Wide Web and is located at the application layer of the TCP/IP protocol stack.
Coding practices
Do you still remember the command at the beginning of the article? To run a website, you need to provide three necessary parameters: Port, website physical path, and website virtual path.
Main( dir =(args.Length== || !.TryParse(args[],= ; = (SimpleHost) ApplicationHost.CreateApplicationHost( (SimpleHost), , dir); WebServer server = InitHostFile( path = Path.Combine(dir, (! source = target = path + + Assembly.GetExecutingAssembly().GetName().Name +
To facilitate the test, I have killed all three parameters. Port 45758 is used by default. The physical path uses the directory where the current program is located, and the virtual path uses the root directory, the two paths are stored in the host object. Because Application. the CreateApplicationHost method expects to find the assembly of the host type in the bin directory of GAC or the specified physical path, therefore, before creating an application domain, copy the current program to the bin directory of the physical path. After creating the application domain, initialize the WebServer object and call the Start method of the object to Start the server. The reference of the host is retained in WebServer. It is used when processing ASP. NET requests. Let's take a look at how to start the server first.
= = = + Port + OnStart(= AcceptSocket(= state =
In the Start method, a global socket object is created to listen to the specified port, and a new thread is created to process client requests. After receiving client requests, hand it over to the HttpProcessor object for processing.
= (requestInfo != staticContentType = (! (requestInfo.FilePath.EndsWith(
The output response is the process of constructing the status line, response header, and response body, and then sending it to the client through socket. I believe that you have an understanding of the entire interaction process, and the last question is how to handle dynamic content.
To interact with ASP. NET application domains, We need to submit the request information to the host Object host for processing. The following is the implemented host class.
PhysicalDir { ; VituralDir { ; Config( vitrualDir, == = WorkerRequest(
FlushResponse( (!-= ( i = ; i < _responseBodyBytes.Count; i++[] data == List<[]>
In this step, we can run ASP. NET program, but only the abstract method can not provide enough information to the HTTP pipeline. For example, the HTTP pipeline cannot know the POST data and Cookie data, to provide this information, we also need to rewrite some virtual methods, such as GetKnownRequestHeader and GetPreloadedEntityBody. After some necessary methods are implemented, ASP. NET program can run well.
Summary
Supports ASP. NET Web server is not difficult, thanks to ASP. NET elegant design, as long as the necessary information is provided to the runtime, the HTTP pipeline can be correctly processed.
Only a small part of the code is posted in this article, you can view all the Code through the https://github.com/lcomplete/AspNetServer that address.