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

Commit

Permalink
Make services registered in ConfigureServices work in Configure
Browse files Browse the repository at this point in the history
- Added a test

#114
  • Loading branch information
davidfowl committed Dec 5, 2014
1 parent d855806 commit 90be46a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Microsoft.AspNet.Hosting/Startup/StartupLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ private MethodInfo FindMethod(Type startupType, string methodName, string enviro

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)
Expand All @@ -74,7 +75,7 @@ private object Invoke(MethodInfo methodInfo, object instance, IApplicationBuilde
{
try
{
parameters[index] = _services.GetRequiredService(parameterInfo.ParameterType);
parameters[index] = serviceProvider.GetRequiredService(parameterInfo.ParameterType);
}
catch (Exception)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
}
17 changes: 17 additions & 0 deletions test/Microsoft.AspNet.Hosting.Tests/StartupManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,23 @@ public void StartupWithNoConfigureThrows()
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);
Expand Down

0 comments on commit 90be46a

Please sign in to comment.