Hosting#698 Remove IServerFactory

This commit is contained in:
Chris R 2016-04-14 14:07:11 -07:00
parent f61af9a746
commit c48353f4ef
5 changed files with 25 additions and 104 deletions

View File

@ -1,13 +0,0 @@
//// 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.Collections.Generic;
using Microsoft.AspNetCore.Server.Features;
namespace Microsoft.AspNetCore.Server.Kestrel.Internal
{
public class ServerAddressesFeature : IServerAddressesFeature
{
public ICollection<string> Addresses { get; } = new List<string>();
}
}

View File

@ -3,13 +3,15 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Server.Features;
using Microsoft.AspNetCore.Server.Kestrel.Http;
using Microsoft.AspNetCore.Server.Kestrel.Infrastructure;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace Microsoft.AspNetCore.Server.Kestrel
{
@ -18,14 +20,10 @@ namespace Microsoft.AspNetCore.Server.Kestrel
private Stack<IDisposable> _disposables;
private readonly IApplicationLifetime _applicationLifetime;
private readonly ILogger _logger;
private readonly IServerAddressesFeature _serverAddresses;
public KestrelServer(IFeatureCollection features, KestrelServerOptions options, IApplicationLifetime applicationLifetime, ILogger logger)
public KestrelServer(IOptions<KestrelServerOptions> options, IApplicationLifetime applicationLifetime, ILoggerFactory loggerFactory)
{
if (features == null)
{
throw new ArgumentNullException(nameof(features));
}
if (options == null)
{
throw new ArgumentNullException(nameof(options));
@ -36,15 +34,19 @@ namespace Microsoft.AspNetCore.Server.Kestrel
throw new ArgumentNullException(nameof(applicationLifetime));
}
if (logger == null)
if (loggerFactory == null)
{
throw new ArgumentNullException(nameof(logger));
throw new ArgumentNullException(nameof(loggerFactory));
}
Options = options.Value ?? new KestrelServerOptions();
_applicationLifetime = applicationLifetime;
_logger = logger;
Features = features;
Options = options;
_logger = loggerFactory.CreateLogger(typeof(KestrelServer).GetTypeInfo().Assembly.FullName);
Features = new FeatureCollection();
var componentFactory = new HttpComponentFactory(Options);
Features.Set<IHttpComponentFactory>(componentFactory);
_serverAddresses = new ServerAddressesFeature();
Features.Set<IServerAddressesFeature>(_serverAddresses);
}
public IFeatureCollection Features { get; }
@ -94,13 +96,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel
engine.Start(threadCount);
var atLeastOneListener = false;
var addressesFeature = Features.Get<IServerAddressesFeature>();
if (addressesFeature == null)
{
throw new InvalidOperationException($"{nameof(IServerAddressesFeature)} is missing.");
}
foreach (var address in addressesFeature.Addresses)
foreach (var address in _serverAddresses.Addresses)
{
var parsedAddress = ServerAddress.FromUrl(address);
if (parsedAddress == null)

View File

@ -1,57 +0,0 @@
// 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 Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Server.Features;
using Microsoft.AspNetCore.Server.Kestrel.Infrastructure;
using Microsoft.AspNetCore.Server.Kestrel.Internal;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace Microsoft.AspNetCore.Server.Kestrel
{
/// <summary>
/// Summary description for ServerFactory
/// </summary>
public class ServerFactory : IServerFactory
{
private readonly IApplicationLifetime _appLifetime;
private readonly ILoggerFactory _loggerFactory;
private readonly KestrelServerOptions _options;
public ServerFactory(IApplicationLifetime appLifetime, ILoggerFactory loggerFactory, IOptions<KestrelServerOptions> optionsAccessor)
{
_appLifetime = appLifetime;
_loggerFactory = loggerFactory;
_options = optionsAccessor.Value;
}
public IServer CreateServer(IConfiguration configuration)
{
var componentFactory = new HttpComponentFactory(_options);
var serverFeatures = new FeatureCollection();
serverFeatures.Set<IHttpComponentFactory>(componentFactory);
serverFeatures.Set(GetAddresses(configuration));
return new KestrelServer(serverFeatures, _options, _appLifetime, _loggerFactory.CreateLogger("Microsoft.AspNetCore.Server.Kestrel"));
}
private static IServerAddressesFeature GetAddresses(IConfiguration configuration)
{
var addressesFeature = new ServerAddressesFeature();
var urls = configuration["server.urls"];
if (!string.IsNullOrEmpty(urls))
{
foreach (var value in urls.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
{
addressesFeature.Addresses.Add(value);
}
}
return addressesFeature;
}
}
}

View File

@ -23,7 +23,7 @@ namespace Microsoft.AspNetCore.Hosting
/// </returns>
public static IWebHostBuilder UseKestrel(this IWebHostBuilder hostBuilder)
{
return hostBuilder.ConfigureServices(services => services.AddSingleton<IServerFactory, ServerFactory>());
return hostBuilder.ConfigureServices(services => services.AddSingleton<IServer, KestrelServer>());
}
/// <summary>

View File

@ -3,11 +3,13 @@
using System;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Server.Features;
using Microsoft.AspNetCore.Server.Kestrel;
using Microsoft.AspNetCore.Server.Kestrel.Infrastructure;
using Microsoft.AspNetCore.Server.Kestrel.Internal;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Xunit;
namespace Microsoft.AspNetCore.Server.KestrelTests
@ -29,9 +31,8 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
[Fact]
public void StartWithInvalidAddressThrows()
{
var addressesFeature = new ServerAddressesFeature();
addressesFeature.Addresses.Add("http:/asdf");
var server = CreateServer(new KestrelServerOptions(), addressesFeature);
var server = CreateServer(new KestrelServerOptions());
server.Features.Get<IServerAddressesFeature>().Addresses.Add("http:/asdf");
var exception = Assert.Throws<FormatException>(() => StartDummyApplication(server));
@ -41,25 +42,19 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
[Fact]
public void StartWithEmptyAddressesThrows()
{
var server = CreateServer(new KestrelServerOptions(), new ServerAddressesFeature());
var server = CreateServer(new KestrelServerOptions());
var exception = Assert.Throws<InvalidOperationException>(() => StartDummyApplication(server));
Assert.Equal("No recognized listening addresses were configured.", exception.Message);
}
private static KestrelServer CreateServer(KestrelServerOptions options, IServerAddressesFeature addressesFeature = null)
private static KestrelServer CreateServer(KestrelServerOptions options)
{
var features = new FeatureCollection();
if (addressesFeature != null)
{
features.Set(addressesFeature);
}
var lifetime = new LifetimeNotImplemented();
var logger = new TestApplicationErrorLogger();
var logger = new LoggerFactory();
return new KestrelServer(features, options, lifetime, logger);
return new KestrelServer(Options.Create(options), lifetime, logger);
}
private static void StartDummyApplication(IServer server)