.net core——(2) asp .net 流程解读

开始之前

在开始之前先了解下web应用的一些概念。
WEB服务器:一个程序,监听某个端口,处理浏览器的请求,向浏览器回馈一个文档(网页)
asp .net core 处理http请求的流程 https://www.cnblogs.com/calvinK/p/6008915.html

Kestrel

Kestrel发音: [‘kestr(?)l]
这是一个web服务器,本质上是个中间件。Kestrel是进程内服务器,以一个包形式提供,自身不能单独运行,必须HOST在一个.NET的WEB应用程序中。它内部封装了对libuv的调用,但不是libuv库简单的封装库。Kestrel是个精简的,高效的Http Server。

疑问:
asp.net core的所有中间件都是进程内中间件?从main启动,所有的中间件都是在Startup类的Configure方法中添加的
asp .net core中间件列表 https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/middleware/?view=aspnetcore-3.1

https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/servers/?view=aspnetcore-3.1&tabs=linux
https://www.cnblogs.com/Leo_wl/p/8405711.html

创建项目

根据官方文档 https://docs.microsoft.com/zh-cn/aspnet/core/getting-started/?view=aspnetcore-3.1&tabs=linux 创建一个简单的项目
创建项目

dotnet new webapp -o aspnetcoreapp

运行

cd aspnetcoreappdotnet watch run

Program.cs

完整代码如下

using System;using System.Collections.Generic;using System.Linq;using System.Threading.Tasks;using Microsoft.AspNetCore.Hosting;using Microsoft.Extensions.Configuration;using Microsoft.Extensions.Hosting;using Microsoft.Extensions.Logging;namespace aspnetcoreapp{ public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }); }}

Startup.cs

默认有两个方法,一个配置管道另一个添加services到容器,当应用启动的时候,这两个方法被runtime调用。
完整代码如下:

using System;using System.Collections.Generic;using System.Linq;using System.Threading.Tasks;using Microsoft.AspNetCore.Builder;using Microsoft.AspNetCore.Hosting;using Microsoft.AspNetCore.HttpsPolicy;using Microsoft.Extensions.Configuration;using Microsoft.Extensions.DependencyInjection;using Microsoft.Extensions.Hosting;namespace aspnetcoreapp{ public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. // 这个方法被runtime调用,使用这个方法添加services到容器 public void ConfigureServices(IServiceCollection services) { services.AddRazorPages(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. // 这个方法被runtime调用,使用这个方法配置HTTP管道 public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Error"); // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapRazorPages(); }); } }}

微软介绍: https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/startup?view=aspnetcore-3.1

反向代理

可以用来做负载均衡。实际就是一个请求转发,浏览器的请求不是直接到web服务器,而是先到反向代理服务器,然后代理服务器转发到web服务器。“反向”的意思是代理在硬件服务器上,正向的话可以理解成xx上网的梯子(代理隐藏了请求的发起者)。

相关文章