Make services registered in ConfigureServices work in Configure
- Added a test #114
This commit is contained in:
parent
2155c08e76
commit
559f5491d4
|
|
@ -57,6 +57,7 @@ namespace Microsoft.AspNet.Hosting.Startup
|
||||||
|
|
||||||
private object Invoke(MethodInfo methodInfo, object instance, IApplicationBuilder builder, IServiceCollection services = null)
|
private object Invoke(MethodInfo methodInfo, object instance, IApplicationBuilder builder, IServiceCollection services = null)
|
||||||
{
|
{
|
||||||
|
var serviceProvider = builder?.ApplicationServices ?? _services;
|
||||||
var parameterInfos = methodInfo.GetParameters();
|
var parameterInfos = methodInfo.GetParameters();
|
||||||
var parameters = new object[parameterInfos.Length];
|
var parameters = new object[parameterInfos.Length];
|
||||||
for (var index = 0; index != parameterInfos.Length; ++index)
|
for (var index = 0; index != parameterInfos.Length; ++index)
|
||||||
|
|
@ -74,7 +75,7 @@ namespace Microsoft.AspNet.Hosting.Startup
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
parameters[index] = _services.GetRequiredService(parameterInfo.ParameterType);
|
parameters[index] = serviceProvider.GetRequiredService(parameterInfo.ParameterType);
|
||||||
}
|
}
|
||||||
catch (Exception)
|
catch (Exception)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -111,6 +111,23 @@ namespace Microsoft.AspNet.Hosting.Tests
|
||||||
Assert.True(ex.Message.Contains("ConfigureBoom or Configure method not found"));
|
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)
|
public void ConfigurationMethodCalled(object instance)
|
||||||
{
|
{
|
||||||
_configurationMethodCalledList.Add(instance);
|
_configurationMethodCalledList.Add(instance);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue