Make services registered in ConfigureServices work in Configure

- Added a test

#114
This commit is contained in:
David Fowler 2014-12-05 06:20:23 -08:00
parent 2155c08e76
commit 559f5491d4
3 changed files with 54 additions and 1 deletions

View File

@ -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)
{

View File

@ -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<IFoo, Foo>();
}
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;
}
}
}
}

View File

@ -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<IStartupManager>();
var app = new ApplicationBuilder(services);
var startup = manager.LoadStartup("Microsoft.AspNet.Hosting.Tests", "WithConfigureServices");
startup.Invoke(app);
var foo = app.ApplicationServices.GetRequiredService<StartupWithConfigureServices.IFoo>();
Assert.True(foo.Invoked);
}
public void ConfigurationMethodCalled(object instance)
{
_configurationMethodCalledList.Add(instance);