Skip to content
This repository has been archived by the owner on Dec 19, 2018. It is now read-only.

Commit

Permalink
Ensure ConfigureServices is only called once
Browse files Browse the repository at this point in the history
  • Loading branch information
HaoK committed May 11, 2015
1 parent 12d3f48 commit 1354d66
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/Microsoft.AspNet.Hosting/Internal/HostingEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,12 @@ public virtual IDisposable Start()

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()
Expand Down
32 changes: 32 additions & 0 deletions test/Microsoft.AspNet.Hosting.Tests/HostingEngineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,38 @@ public void HostingEngine_DoesNot_CreateDefaultRequestIdentifierFeature_IfPresen
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>();
Expand Down

0 comments on commit 1354d66

Please sign in to comment.