diff --git a/src/Microsoft.AspNet.Hosting/Startup/StartupLoader.cs b/src/Microsoft.AspNet.Hosting/Startup/StartupLoader.cs index c76153e9d0..8063d48c44 100644 --- a/src/Microsoft.AspNet.Hosting/Startup/StartupLoader.cs +++ b/src/Microsoft.AspNet.Hosting/Startup/StartupLoader.cs @@ -57,6 +57,7 @@ namespace Microsoft.AspNet.Hosting.Startup private object Invoke(MethodInfo methodInfo, object instance, IApplicationBuilder builder, IServiceCollection services = null) { + var serviceProvider = builder?.ApplicationServices ?? _services; var parameterInfos = methodInfo.GetParameters(); var parameters = new object[parameterInfos.Length]; for (var index = 0; index != parameterInfos.Length; ++index) @@ -74,7 +75,7 @@ namespace Microsoft.AspNet.Hosting.Startup { try { - parameters[index] = _services.GetRequiredService(parameterInfo.ParameterType); + parameters[index] = serviceProvider.GetRequiredService(parameterInfo.ParameterType); } catch (Exception) { diff --git a/test/Microsoft.AspNet.Hosting.Tests/Fakes/StartupWithConfigureServices.cs b/test/Microsoft.AspNet.Hosting.Tests/Fakes/StartupWithConfigureServices.cs new file mode 100644 index 0000000000..8c580bb13b --- /dev/null +++ b/test/Microsoft.AspNet.Hosting.Tests/Fakes/StartupWithConfigureServices.cs @@ -0,0 +1,35 @@ +using System; +using Microsoft.AspNet.Builder; +using Microsoft.Framework.DependencyInjection; + +namespace Microsoft.AspNet.Hosting.Fakes +{ + public class StartupWithConfigureServices + { + public void ConfigureServices(IServiceCollection services) + { + services.AddSingleton(); + } + + public void Configure(IApplicationBuilder app, IFoo foo) + { + foo.Bar(); + } + + public interface IFoo + { + bool Invoked { get; } + void Bar(); + } + + public class Foo : IFoo + { + public bool Invoked { get; private set; } + + public void Bar() + { + Invoked = true; + } + } + } +} \ No newline at end of file diff --git a/test/Microsoft.AspNet.Hosting.Tests/StartupManagerTests.cs b/test/Microsoft.AspNet.Hosting.Tests/StartupManagerTests.cs index 10016cdd1b..d9789bfccf 100644 --- a/test/Microsoft.AspNet.Hosting.Tests/StartupManagerTests.cs +++ b/test/Microsoft.AspNet.Hosting.Tests/StartupManagerTests.cs @@ -111,6 +111,23 @@ namespace Microsoft.AspNet.Hosting.Tests Assert.True(ex.Message.Contains("ConfigureBoom or Configure method not found")); } + [Fact] + public void StartupClassWithConfigureServicesShouldMakeServiceAvailableInConfigure() + { + var serviceCollection = HostingServices.Create(); + var services = serviceCollection.BuildServiceProvider(); + var manager = services.GetRequiredService(); + + var app = new ApplicationBuilder(services); + + var startup = manager.LoadStartup("Microsoft.AspNet.Hosting.Tests", "WithConfigureServices"); + + startup.Invoke(app); + + var foo = app.ApplicationServices.GetRequiredService(); + Assert.True(foo.Invoked); + } + public void ConfigurationMethodCalled(object instance) { _configurationMethodCalledList.Add(instance);