Add KestrelServerOptionsSetup to IServiceCollection in UseKestrel() (#755)

- Previously, KestrelServerOptionsSetup was only added to IServiceCollection in UseKestrel(options)
- Required to ensure that options.ApplicationServices is available after calling UseKestrel()
This commit is contained in:
Mike Harder 2016-04-18 15:32:09 -07:00
parent c48353f4ef
commit bbf2c83a7d
2 changed files with 40 additions and 2 deletions

View File

@ -23,7 +23,11 @@ namespace Microsoft.AspNetCore.Hosting
/// </returns>
public static IWebHostBuilder UseKestrel(this IWebHostBuilder hostBuilder)
{
return hostBuilder.ConfigureServices(services => services.AddSingleton<IServer, KestrelServer>());
return hostBuilder.ConfigureServices(services =>
{
services.AddTransient<IConfigureOptions<KestrelServerOptions>, KestrelServerOptionsSetup>();
services.AddSingleton<IServer, KestrelServer>();
});
}
/// <summary>
@ -42,7 +46,6 @@ namespace Microsoft.AspNetCore.Hosting
{
hostBuilder.ConfigureServices(services =>
{
services.AddTransient<IConfigureOptions<KestrelServerOptions>, KestrelServerOptionsSetup>();
services.Configure(options);
});

View File

@ -0,0 +1,35 @@
// 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.Server.Kestrel;
using Microsoft.Extensions.DependencyInjection;
using Xunit;
namespace Microsoft.AspNetCore.Server.KestrelTests
{
public class WebHostBuilderKestrelExtensionsTests
{
[Fact]
public void ApplicationServicesNotNullAfterUseKestrelWithoutOptions()
{
// Arrange
var hostBuilder = new WebHostBuilder()
.UseKestrel()
.Configure(app => { });
hostBuilder.ConfigureServices(services =>
{
services.Configure<KestrelServerOptions>(options =>
{
// Assert
Assert.NotNull(options.ApplicationServices);
});
});
// Act
var host = hostBuilder.Build();
}
}
}