08-005 Hosting 之 HostingEngine : IHostingEngine
Posted on 2015-04-07 12:35 DotNet1010 阅读(169) 评论(0) 收藏 举报接口代码:
public interface IHostingEngine
{
IDisposable Start(HostingContext context);
}
核心方法:
public IDisposable Start(HostingContext context)
{
// context.Builder = _builderFactory.CreateBuilder();
EnsureBuilder(context);
// context.ServerFactory = _serverManager.GetServerFactory(context.ServerName);
EnsureServerFactory(context);
// context.Server = context.ServerFactory.Initialize(context.Configuration);
// context.Builder.Server = context.Server;
InitalizeServerFactory(context);
/*
context.ApplicationStartup = _startupManager.LoadStartup(
context.ApplicationName,
context.EnvironmentName);
context.ApplicationStartup.Invoke(context.Builder); -- 这里才调用两个核心方法
context.ApplicationDelegate = context.Builder.Build(); --生成RequestDelegate方法
用来处理 HttpContext
*/
EnsureApplicationDelegate(context);
var applicationLifetime = (ApplicationLifetime)context.Services.GetRequiredService<IApplicationLifetime>();
// 创建 HttpContext 并进行执行
var pipeline = new PipelineInstance(_httpContextFactory, context.ApplicationDelegate, _contextAccessor);
/*
Start(IServerInformation serverInformation, Func<object, Task> application)
请求信息环境等信息 封装为Object 到 实现 IServerFactory 的用户自定义类;
用户自定义类 调用 假定有此方法: Task Invoke( 包含请求环境信息的Object );
然后调用 PipelineInstance.Invoke serverEnvironment 处理后为 HttpContext
继续调用 context.ApplicationDelegate = context.Builder.Build() 方法处理 HttpContext
PipelineInstance.Invoke 代码如下:
public Task Invoke(object serverEnvironment)
{
var httpContext = _httpContextFactory.CreateHttpContext(serverEnvironment);
_contextAccessor.Value = httpContext;
return _requestDelegate(httpContext);
}
*/
// 这里简单理解为 服务启动了 比如启动IIS 具体怎么做要看服务是怎么实现了
var server = context.ServerFactory.Start(context.Server, pipeline.Invoke);
return new Disposable(() =>
{
applicationLifetime.SignalStopping();
server.Dispose();
pipeline.Dispose();
applicationLifetime.SignalStopped();
});
}
浙公网安备 33010602011771号