Ensure ConfigureServices is only called once

This commit is contained in:
Hao Kung 2015-05-11 15:08:40 -07:00
parent 12d3f48bfd
commit 1354d66fb6
2 changed files with 38 additions and 5 deletions

View File

@ -91,11 +91,12 @@ namespace Microsoft.AspNet.Hosting.Internal
private void EnsureApplicationServices()
{
EnsureStartup();
_applicationServiceCollection.AddInstance<IApplicationLifetime>(_applicationLifetime);
_applicationServices = Startup.ConfigureServicesDelegate(_applicationServiceCollection);
if (_applicationServices == null)
{
EnsureStartup();
_applicationServiceCollection.AddInstance<IApplicationLifetime>(_applicationLifetime);
_applicationServices = Startup.ConfigureServicesDelegate(_applicationServiceCollection);
}
}
private void EnsureStartup()

View File

@ -291,6 +291,38 @@ namespace Microsoft.AspNet.Hosting
Assert.Same(requestIdentifierFeature, httpContext.GetFeature<IRequestIdentifierFeature>());
}
[Fact]
public void HostingEngine_InvokesConfigureMethodsOnlyOnce()
{
var engine = CreateBuilder()
.UseServer(this)
.UseStartup<CountStartup>()
.Build();
using (engine.Start())
{
var services = engine.ApplicationServices;
var services2 = engine.ApplicationServices;
Assert.Equal(1, CountStartup.ConfigureCount);
Assert.Equal(1, CountStartup.ConfigureServicesCount);
}
}
public class CountStartup
{
public static int ConfigureServicesCount;
public static int ConfigureCount;
public void ConfigureServices(IServiceCollection services)
{
ConfigureServicesCount++;
}
public void Configure(IApplicationBuilder app)
{
ConfigureCount++;
}
}
private IHostingEngine CreateHostingEngine(RequestDelegate requestDelegate)
{
var applicationBuilder = new Mock<IApplicationBuilder>();