上篇講到.net core web app是如何啟動(dòng)并接受請(qǐng)求的,下面接著探索kestrel server是如何完成此任務(wù)的。
1.kestrel server的入口KestrelServer.Start(Microsoft.AspNetCore.Hosting.Server.IHttpApplication)
FrameFactory創(chuàng)建的frame實(shí)例最終會(huì)交給libuv的loop回調(diào)接收請(qǐng)求。但是在這過(guò)程中還是有很多的初始化工作需要做的。后面我們就管中窺豹來(lái)看一看。
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 }); //啟動(dòng)引擎。完成libuv的配置和啟動(dòng) engine.Start(threadCount); //針對(duì)綁定的多個(gè)地址創(chuàng)建server來(lái)接收請(qǐng)求。也就是針對(duì)ip:port來(lái)啟動(dòng)tcp監(jiān)聽(tīng) foreach (var address in _serverAddresses.Addresses.ToArray()) { engine.CreateServer(ipv4Address);