ASP.NET 应程序运行结构

运行环境

Asp.Net 所设计的类主要定义在System.Web程序集下,通过将应用程序集加载到应用程序域AppDomain运行。AppDomain为运行环境。

  • 优点:不需要创建多个进程,一个进程中可以包含多个应用程序域,独立性,一个应用程序域挂掉不会影响其他应用程序域。

    面向对象化处理请求

    Asp.net为了方便处理,将客户端请信息和响应信息对象化了,这样我们开发中只需要读取或设置对象属性,调用对象方法处理客户端请求。
    System.Web.HttpRuntime类是Asp.Net处理入口,每个应用程序域存在一个。
  1. 其中HttpRuntime的静态方法ProcessRequest接受HttpWorkRequest
  2. HttpWorkRequest 的实例提供处理请求说需要的信息
  3. 根据HttpWorkRequest实例数据,创建HttpRequest用于获取客户端请求 和HttpResponse用于服务端响应。
  • HttpRequest对象
Properties description
HttpMethod 请求方式
RawUrl 原始请求地址
QueryString 请求参数(NameValueCollection类型)
Cookies 请求的Cookie
Form 请求的表单
Files 上传的文件
  • HttpResponse对象
Properties description
StatusCode 设置状态码
ContentType 设置响应文件类型
AppendHeader 方式设置响应头
outputStream 输出流,写入流响应客户端
  • HttpServerUtility 辅助工具类
Properties description
MapPath 将虚拟路径转换为物理路径
HtmlEncode Html标签编码
UrllEncode Url标签编码

一个简单的ASP.Net Server

新建类库,程序集为ASPNET.Server,下新建类

    public class WebServer : MarshalByRefObject
    {
        public void ProcessRequest(string page, string query, TextWriter writer)
        {
            SimpleWorkerRequest worker = new SimpleWorkerRequest(page, query, writer);
            HttpRuntime.ProcessRequest(worker);
        }
    }

新建监听程序(控制台应用程序),运行时需要在.exe路径下新建bin文件夹并保存ASPNET.Server.dll文件,这里通过ApplicationHost.CreateApplicationHost创建程序域,将ASPNET.Server.dll程序动态加载到程序域。

public delegate void ProcessRequestHandler(string page, string query, TextWriter writer);
    public class HttpServer
    {
        public event ProcessRequestHandler ProcessRequest;
        public void Run()
        {
            if (!HttpListener.IsSupported)
            {
                Console.WriteLine("Not Support");
            }

            string[] prefixes = new string[] { "http://localhost:8085/" };

            HttpListener listener = new HttpListener();
            foreach (string s in prefixes)
            {
                listener.Prefixes.Add(s);
            }
            //开启Http服务监听
            listener.Start();
            Console.WriteLine("Listening...");
            while (true)
            {
                //接受客户端请求数据
                HttpListenerContext context = listener.GetContext();
                // 提取客户端请求对象
                HttpListenerRequest request = context.Request;

                // 服务端响应
                HttpListenerResponse response = context.Response;
                
                //out stream
                using(TextWriter writer = new StreamWriter(response.OutputStream))
                {
                    string path = Path.GetFileName(request.Url.AbsolutePath);
                    StringWriter sw = new StringWriter();
                    this.ProcessRequest.Invoke(path, request.Url.Query, sw);
                    //获取
                    string content = sw.ToString();
                    sw.Close();
                    Console.WriteLine(content);
                    response.ContentLength64 = Encoding.UTF8.GetByteCount(content);
                    response.ContentType = "text/html;charset=UTF-8";
                    writer.Write(content);
                    Console.WriteLine("\r\n Process OK.\r\n");
                }
            
                if (Console.KeyAvailable)
                {
                    break;
                }
            }
            listener.Stop();
        }
    }

运行程序

        static void Main(string[] args)
        {
            Type type = typeof(ASPNET.Server.WebServer);
            WebServer server = ApplicationHost.CreateApplicationHost(type, "/", System.Environment.CurrentDirectory) as WebServer;
            HttpServer listener = new HttpServer();
            listener.ProcessRequest += server.ProcessRequest;
            listener.Run();

            Console.ReadKey();
        }

猜你喜欢

转载自www.cnblogs.com/LoveTomato/p/9106947.html
今日推荐