上篇講到.net core web app是如何啟動并接受請求的,下面接著探索kestrel server是如何完成此任務(wù)的。
1.kestrel server的入口KestrelServer.Start(Microsoft.AspNetCore.Hosting.Server.IHttpApplication)
FrameFactory創(chuàng)建的frame實(shí)例最終會交給libuv的loop回調(diào)接收請求。但是在這過程中還是有很多的初始化工作需要做的。后面我們就管中窺豹來看一看。
public void Start<TContext>(IHttpApplication<TContext> application) { var engine = new KestrelEngine(new ServiceContext { FrameFactory = context => { return new Frame<TContext>(application, context); }, AppLifetime = _applicationLifetime, Log = trace, ThreadPool = new LoggingThreadPool(trace), DateHeaderValueManager = dateHeaderValueManager, ServerOptions = Options }); //啟動引擎。完成libuv的配置和啟動 engine.Start(threadCount); //針對綁定的多個地址創(chuàng)建server來接收請求。也就是針對ip:port來啟動tcp監(jiān)聽 foreach (var address in _serverAddresses.Addresses.ToArray()) { engine.CreateServer(ipv4Address);