Kestrel 是什么?
Kestrel 是一个开源跨平台的轻量级Web服务,Asp.net core应用默认使用Kestrel作为处理网络请求的服务。Kestrel是用Nodejs开发的基于异步I/O的库
Kestrel有以下特性:
- 不支持反向代理服务(如:IIS,Apache,Nginx)的大部分特性
- Kestrel是跨平台的,可以运行在Windows,Linux和Mac平台上
- 支持SSL
- 支持与方向代理建立套接字连接,提高性能
在有了IIS/Nginx/Apache后为什么还要使用Kestrel
没有Kestrel的话,Asp.net core应用为了满足在IIS/Nginx/Apache中部署,需要在应用中为不同的Web容器配置不同的启动设置。而Kestrel可以让你的应用程序按照同样的标准(main(),Startup.ConfigureServices,Startup.Configure)配置启动,也就是跨平台特性
如何使用Kestrel
public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.UseApplicationInsights()
.Build();
host.Run();
}
}
注意
如果你不设置UseUrls("http://localhost:5000", "http://*:5000", "http://192.168.1.2:5000")
,Kestrel只会监听本机的请求,换句话就是,你不能在外部网络访问Host应用。如果你设置"http://*:5000"
,则Host应用允许外部网络访问