Merge branch 'release' into dev

This commit is contained in:
Chris R 2016-04-15 16:06:15 -07:00
commit 20a1f4a548
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;
using System.Collections.Generic; using System.Collections.Generic;
using System.Reflection;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Server; using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.AspNetCore.Http.Features; using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Server.Features;
using Microsoft.AspNetCore.Server.Kestrel.Http; using Microsoft.AspNetCore.Server.Kestrel.Http;
using Microsoft.AspNetCore.Server.Kestrel.Infrastructure; using Microsoft.AspNetCore.Server.Kestrel.Infrastructure;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace Microsoft.AspNetCore.Server.Kestrel namespace Microsoft.AspNetCore.Server.Kestrel
{ {
@ -18,14 +20,10 @@ namespace Microsoft.AspNetCore.Server.Kestrel
private Stack<IDisposable> _disposables; private Stack<IDisposable> _disposables;
private readonly IApplicationLifetime _applicationLifetime; private readonly IApplicationLifetime _applicationLifetime;
private readonly ILogger _logger; 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) if (options == null)
{ {
throw new ArgumentNullException(nameof(options)); throw new ArgumentNullException(nameof(options));
@ -36,15 +34,19 @@ namespace Microsoft.AspNetCore.Server.Kestrel
throw new ArgumentNullException(nameof(applicationLifetime)); 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; _applicationLifetime = applicationLifetime;
_logger = logger; _logger = loggerFactory.CreateLogger(typeof(KestrelServer).GetTypeInfo().Assembly.FullName);
Features = features; Features = new FeatureCollection();
Options = options; var componentFactory = new HttpComponentFactory(Options);
Features.Set<IHttpComponentFactory>(componentFactory);
_serverAddresses = new ServerAddressesFeature();
Features.Set<IServerAddressesFeature>(_serverAddresses);
} }
public IFeatureCollection Features { get; } public IFeatureCollection Features { get; }
@ -94,13 +96,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel
engine.Start(threadCount); engine.Start(threadCount);
var atLeastOneListener = false; var atLeastOneListener = false;
var addressesFeature = Features.Get<IServerAddressesFeature>(); foreach (var address in _serverAddresses.Addresses)
if (addressesFeature == null)
{
throw new InvalidOperationException($"{nameof(IServerAddressesFeature)} is missing.");
}
foreach (var address in addressesFeature.Addresses)
{ {
var parsedAddress = ServerAddress.FromUrl(address); var parsedAddress = ServerAddress.FromUrl(address);
if (parsedAddress == null) 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> /// </returns>
public static IWebHostBuilder UseKestrel(this IWebHostBuilder hostBuilder) public static IWebHostBuilder UseKestrel(this IWebHostBuilder hostBuilder)
{ {
return hostBuilder.ConfigureServices(services => services.AddSingleton<IServerFactory, ServerFactory>()); return hostBuilder.ConfigureServices(services => services.AddSingleton<IServer, KestrelServer>());
} }
/// <summary> /// <summary>

View File

@ -3,11 +3,13 @@
using System; using System;
using Microsoft.AspNetCore.Hosting.Server; using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.AspNetCore.Http.Features; using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Server.Features;
using Microsoft.AspNetCore.Server.Kestrel; using Microsoft.AspNetCore.Server.Kestrel;
using Microsoft.AspNetCore.Server.Kestrel.Infrastructure; using Microsoft.AspNetCore.Server.Kestrel.Infrastructure;
using Microsoft.AspNetCore.Server.Kestrel.Internal; using Microsoft.AspNetCore.Server.Kestrel.Internal;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Xunit; using Xunit;
namespace Microsoft.AspNetCore.Server.KestrelTests namespace Microsoft.AspNetCore.Server.KestrelTests
@ -29,9 +31,8 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
[Fact] [Fact]
public void StartWithInvalidAddressThrows() public void StartWithInvalidAddressThrows()
{ {
var addressesFeature = new ServerAddressesFeature(); var server = CreateServer(new KestrelServerOptions());
addressesFeature.Addresses.Add("http:/asdf"); server.Features.Get<IServerAddressesFeature>().Addresses.Add("http:/asdf");
var server = CreateServer(new KestrelServerOptions(), addressesFeature);
var exception = Assert.Throws<FormatException>(() => StartDummyApplication(server)); var exception = Assert.Throws<FormatException>(() => StartDummyApplication(server));
@ -41,25 +42,19 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
[Fact] [Fact]
public void StartWithEmptyAddressesThrows() public void StartWithEmptyAddressesThrows()
{ {
var server = CreateServer(new KestrelServerOptions(), new ServerAddressesFeature()); var server = CreateServer(new KestrelServerOptions());
var exception = Assert.Throws<InvalidOperationException>(() => StartDummyApplication(server)); var exception = Assert.Throws<InvalidOperationException>(() => StartDummyApplication(server));
Assert.Equal("No recognized listening addresses were configured.", exception.Message); 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 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) private static void StartDummyApplication(IServer server)