// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNet.FeatureModel;
using Microsoft.AspNet.Hosting.Server;
using Microsoft.AspNet.Server.Kestrel;
using Microsoft.Framework.Configuration;
using Microsoft.Framework.Runtime;
namespace Kestrel
{
///
/// Summary description for ServerFactory
///
public class ServerFactory : IServerFactory
{
private readonly ILibraryManager _libraryManager;
private readonly IApplicationShutdown _appShutdownService;
public ServerFactory(ILibraryManager libraryManager, IApplicationShutdown appShutdownService)
{
_libraryManager = libraryManager;
_appShutdownService = appShutdownService;
}
public IServerInformation Initialize(IConfiguration configuration)
{
var information = new ServerInformation();
information.Initialize(configuration);
return information;
}
public IDisposable Start(IServerInformation serverInformation, Func application)
{
var disposables = new List();
var information = (ServerInformation)serverInformation;
var engine = new KestrelEngine(_libraryManager, _appShutdownService);
engine.Start(1);
foreach (var address in information.Addresses)
{
disposables.Add(engine.CreateServer(
address.Scheme,
address.Host,
address.Port,
async frame =>
{
var request = new ServerRequest(frame);
await application.Invoke(request.Features);
}));
}
disposables.Add(engine);
return new Disposable(() =>
{
foreach (var disposable in disposables)
{
disposable.Dispose();
}
});
}
}
}