WebHost.CreateDefaultBuilder(args)的源码

public static IWebHostBuilder CreateDefaultBuilder(string[] args)
        {
            //创建一个web宿主的builder
            var builder = new WebHostBuilder()
            //使用Kestrel作为宿主
                .UseKestrel()
            //获取当前web项目的目录
                .UseContentRoot(Directory.GetCurrentDirectory())
            //配置webApp
                .ConfigureAppConfiguration((hostingContext, config) =>
                {
            //env得到环境变量
                    var env = hostingContext.HostingEnvironment;
            //config来获取appsettings.json的内容并配置
                    config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                          .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
             //如果是开发者环境
                    if (env.IsDevelopment())
                    {
             //加载开发者环境的配置
                        var appAssembly = Assembly.Load(new AssemblyName(env.ApplicationName));
                        if (appAssembly != null)
                        {
                            config.AddUserSecrets(appAssembly, optional: true);
                        }
                    }
              //config配置环境变量参数
                    config.AddEnvironmentVariables();
 
                    if (args != null)
                    {
                        config.AddCommandLine(args);
                    }
                })
               //配置logger
                .ConfigureLogging((hostingContext, logging) =>
                {
                    logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
                    logging.AddConsole();
                    logging.AddDebug();
                })
                //用IIS托管webapp
                .UseIISIntegration()
                .UseDefaultServiceProvider((context, options) =>
                {
                    options.ValidateScopes = context.HostingEnvironment.IsDevelopment();
                });

            return builder;
        }

asp.net core 自带了两种http server
一个是WebListener,只能用于window系统。
一个是Kestrel,他是跨平台的。(默认的web server)。

但我们开发使用的是IIS EXpress,调用UseIISIntergration()来启动IIS Express,他作为Kestrel的反向代理使用。

当然也可以单独使用kestrel作为web 服务器, 但是使用iis作为reverse proxy还是由很多有点的: 例如,IIS可以过滤请求, 管理证书, 程序崩溃时自动重启等.

如果在windows服务器上部署的话, 就应该使用IIS作为Kestrel的反向代理服务器来管理和代理请求.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值