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的反向代理服务器来管理和代理请求.