• ASP.NET 启动和运行机制


    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>();
                    });
        }
    
    1. Main:程序的起点,.Net Core 应用程序本质上是控制台应用程序
    2. CreateDefaultBuilder: 创建并配置WebHostBuilder, 首先调用Create­DefaultBuilder, 进行一系列配置。
    3. UseStartup:指定StartUp为启动配置文件。在StartUp中做两个重要的工作
      • ConfigureServices方法是注册服务
      • Configure方法是配置管道,用来具体指定如何处理每个http请求的, 例如我们可以让这个程序知道我使用mvc来处理http请求, 那就调用app.UseMvc()这个方法就行.
    4. BuildWebHost:生成WebHostBuilder并进行了一系列配置之后, 通过CreateHostBuilder(args)对象来Build出一个IHostBuilder。
    5. 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

    关注公众号不迷路

    公众号

  • 相关阅读:
    【C#高级】泛型(一)
    【C#】RGB转CMYK
    C#读写Excel
    H5+MUI上传文件
    完整登录流程包含第三方登录,很详细值得推荐
    [MVC]多文件、超大文件上传
    [SQL Server] 无法连接到本地数据库
    “System.OutOfMemoryException”类型的异常在 mscorlib.dll 中发生,但未在用户代码中进行处理
    MVC缓存(二)
    MVC缓存(一)
  • 原文地址:https://www.cnblogs.com/yuyue5945/p/14149168.html
Copyright © 2020-2023  润新知