ASP.NET Core
- ASP.NET Core的运行机制
- ASP .NET Core 的启动
- ASP .NET Core 管道中间件
ASP.NET Core的运行机制
图示阐明
1、Web server: ASP.NET CORE 提供两种服务器可用:kestrel and HTTP.sys
- kestrel 是一个跨平台的Web服务器
- HTTP.sys 只能用在Windows中
2、Internet : 广域网中,须要windows验证的时候,能够抉择HTTP.sys
3、IIS、Apache、Nginx:反向代理服务器
ASP .NET Core 的启动
启动流程图如下
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>(); }); }
- Main:程序的终点,.Net Core 应用程序实质上是控制台应用程序
- CreateDefaultBuilder: 创立并配置WebHostBuilder, 首先调用CreateDefaultBuilder, 进行一系列配置。
UseStartup:指定StartUp为启动配置文件。在StartUp中做两个重要的工作
- ConfigureServices办法是注册服务
- Configure办法是配置管道,用来具体指定如何解决每个http申请的, 例如咱们能够让这个程序晓得我应用mvc来解决http申请, 那就调用app.UseMvc()这个办法就行.
- BuildWebHost:生成WebHostBuilder并进行了一系列配置之后, 通过CreateHostBuilder(args)对象来Build出一个IHostBuilder。
- Run:调用IWebHost的Run办法使之开始运行。
反向编译进入CreateDefaulBuilder办法中能够看到相重点关键字
public static IHostBuilder CreateDefaultBuilder(string[] args) { HostBuilder hostBuilder = new HostBuilder(); hostBuilder.UseContentRoot(Directory.GetCurrentDirectory()); hostBuilder.ConfigureHostConfiguration((Action<IConfigurationBuilder>)(config => { config.AddEnvironmentVariables("DOTNET_"); if (args == null) return; config.AddCommandLine(args); })); hostBuilder.ConfigureAppConfiguration((Action<HostBuilderContext, IConfigurationBuilder>)((hostingContext, config) => { IHostEnvironment hostingEnvironment = hostingContext.HostingEnvironment; bool reloadOnChange = hostingContext.Configuration.GetValue<bool>("hostBuilder:reloadConfigOnChange", true); config.AddJsonFile("appsettings.json", true, reloadOnChange).AddJsonFile("appsettings." + hostingEnvironment.EnvironmentName + ".json", true, reloadOnChange); if (hostingEnvironment.IsDevelopment() && !string.IsNullOrEmpty(hostingEnvironment.ApplicationName)) { Assembly assembly = Assembly.Load(new AssemblyName(hostingEnvironment.ApplicationName)); if (assembly != (Assembly)null) UserSecretsConfigurationExtensions.AddUserSecrets(config, assembly, true); } config.AddEnvironmentVariables(); if (args == null) return; config.AddCommandLine(args); })).ConfigureLogging((Action<HostBuilderContext, ILoggingBuilder>)((hostingContext, logging) => { bool flag = RuntimeInformation.IsOSPlatform(OSPlatform.Windows); if (flag) logging.AddFilter<EventLogLoggerProvider>((Func<LogLevel, bool>)(level => level >= LogLevel.Warning)); logging.AddConfiguration((IConfiguration)hostingContext.Configuration.GetSection("Logging")); logging.AddConsole(); logging.AddDebug(); logging.AddEventSourceLogger(); if (flag) logging.AddEventLog(); logging.Configure((Action<LoggerFactoryOptions>)(options => options.ActivityTrackingOptions = ActivityTrackingOptions.SpanId | ActivityTrackingOptions.TraceId | ActivityTrackingOptions.ParentId)); })).UseDefaultServiceProvider((Action<HostBuilderContext, ServiceProviderOptions>)((context, options) => { bool flag = context.HostingEnvironment.IsDevelopment(); options.ValidateScopes = flag; options.ValidateOnBuild = flag; })); return (IHostBuilder)hostBuilder; }
UseKestrel 指定服务器应用 Kestrel,若应用HttpSys,需应用UseHttpSys。
UseContentRoot 指定根目录
ConfigureAppConfiguration 读取配置文件
ConfigureLogging 配置日志处理程序
UseIISIntegration 将应用程序配置为在 IIS 中运行。如果应用程序没有应用 IIS 作为反向代理,那么 UseIISIntegration 不会有任何成果。因而,即便应用程序在非 IIS 计划中运行,也能够平安调用这种办法。
UseDefaultServiceProvider 设置默认的依赖注入容器。
ASP .NET Core 管道中间件
申请管道 解决http requests并返回responses的代码组成了request pipeline(申请管道).
中间件: 咱们能够应用一些程序来配置申请管道(request pipeline)以便解决requests和responses. 比方解决验证(authentication)的程序, MVC自身就是个中间件(middleware).
当接管到一个申请时,申请会交给中间件形成的中间件管道进行解决,管道就是多个中间件形成,申请从一个中间件的一端进入,从中间件的另一端进去,每个中间件都能够对HttpContext申请开始和完结进行解决.
## 博主GitHub地址
https://github.com/yuyue5945