diff --git a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/ServerAddressesFeature.cs b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/ServerAddressesFeature.cs deleted file mode 100644 index 6294923d6f..0000000000 --- a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/ServerAddressesFeature.cs +++ /dev/null @@ -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 Addresses { get; } = new List(); - } -} diff --git a/src/Microsoft.AspNetCore.Server.Kestrel/KestrelServer.cs b/src/Microsoft.AspNetCore.Server.Kestrel/KestrelServer.cs index 636710dd72..1a2b97f68d 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel/KestrelServer.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel/KestrelServer.cs @@ -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 _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 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(componentFactory); + _serverAddresses = new ServerAddressesFeature(); + Features.Set(_serverAddresses); } public IFeatureCollection Features { get; } @@ -94,13 +96,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel engine.Start(threadCount); var atLeastOneListener = false; - var addressesFeature = Features.Get(); - 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) diff --git a/src/Microsoft.AspNetCore.Server.Kestrel/ServerFactory.cs b/src/Microsoft.AspNetCore.Server.Kestrel/ServerFactory.cs deleted file mode 100644 index ada5a92948..0000000000 --- a/src/Microsoft.AspNetCore.Server.Kestrel/ServerFactory.cs +++ /dev/null @@ -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 description for ServerFactory - /// - public class ServerFactory : IServerFactory - { - private readonly IApplicationLifetime _appLifetime; - private readonly ILoggerFactory _loggerFactory; - private readonly KestrelServerOptions _options; - - public ServerFactory(IApplicationLifetime appLifetime, ILoggerFactory loggerFactory, IOptions optionsAccessor) - { - _appLifetime = appLifetime; - _loggerFactory = loggerFactory; - _options = optionsAccessor.Value; - } - - public IServer CreateServer(IConfiguration configuration) - { - var componentFactory = new HttpComponentFactory(_options); - var serverFeatures = new FeatureCollection(); - serverFeatures.Set(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; - } - - } -} diff --git a/src/Microsoft.AspNetCore.Server.Kestrel/WebHostBuilderKestrelExtensions.cs b/src/Microsoft.AspNetCore.Server.Kestrel/WebHostBuilderKestrelExtensions.cs index 7632647d03..edebf2a94b 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel/WebHostBuilderKestrelExtensions.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel/WebHostBuilderKestrelExtensions.cs @@ -23,7 +23,7 @@ namespace Microsoft.AspNetCore.Hosting /// public static IWebHostBuilder UseKestrel(this IWebHostBuilder hostBuilder) { - return hostBuilder.ConfigureServices(services => services.AddSingleton()); + return hostBuilder.ConfigureServices(services => services.AddSingleton()); } /// diff --git a/test/Microsoft.AspNetCore.Server.KestrelTests/KestrelServerTests.cs b/test/Microsoft.AspNetCore.Server.KestrelTests/KestrelServerTests.cs index 89b828f468..7db58f52a6 100644 --- a/test/Microsoft.AspNetCore.Server.KestrelTests/KestrelServerTests.cs +++ b/test/Microsoft.AspNetCore.Server.KestrelTests/KestrelServerTests.cs @@ -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().Addresses.Add("http:/asdf"); var exception = Assert.Throws(() => 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(() => 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)