Skip to content

Commit fe3675a

Browse files
committed
Ability to derive Startup from a base class and have "Configure" and "ConfigureServices" be invoked from the base class when present.
1 parent 6208698 commit fe3675a

File tree

4 files changed

+47
-17
lines changed

4 files changed

+47
-17
lines changed

src/Microsoft.AspNet.Hosting/Startup/StartupLoader.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,18 @@ private MethodInfo FindMethod(Type startupType, string methodName, string enviro
2929
{
3030
var methodNameWithEnv = string.Format(CultureInfo.InvariantCulture, methodName, environmentName);
3131
var methodNameWithNoEnv = string.Format(CultureInfo.InvariantCulture, methodName, "");
32-
var methodInfo = startupType.GetTypeInfo().GetDeclaredMethod(methodNameWithEnv)
33-
?? startupType.GetTypeInfo().GetDeclaredMethod(methodNameWithNoEnv);
34-
if (methodInfo == null)
32+
var methodInfo = startupType.GetMethod(methodNameWithEnv, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static)
33+
?? startupType.GetMethod(methodNameWithNoEnv, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
34+
if (methodInfo == null)
3535
{
3636
if (required)
37-
{
38-
throw new Exception(string.Format("TODO: {0} or {1} method not found",
39-
methodNameWithEnv,
40-
methodNameWithNoEnv));
37+
{
38+
throw new Exception(string.Format("TODO: {0} or {1} method not found",
39+
methodNameWithEnv,
40+
methodNameWithNoEnv));
4141

42-
}
43-
return null;
42+
}
43+
return null;
4444
}
4545
if (returnType != null && methodInfo.ReturnType != returnType)
4646
{
@@ -160,7 +160,7 @@ public Action<IApplicationBuilder> LoadStartup(
160160
}
161161
else
162162
{
163-
// void ConfigureServices(IServiceCollection)
163+
// void ConfigureServices(IServiceCollection)
164164
Invoke(servicesMethod, instance, builder, services);
165165
if (builder != null)
166166
{

test/Microsoft.AspNet.Hosting.Tests/Fakes/Startup.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
namespace Microsoft.AspNet.Hosting.Fakes
1111
{
12-
public class Startup
12+
public class Startup : StartupBase
1313
{
1414
public Startup()
1515
{
@@ -31,7 +31,7 @@ public void ConfigureDevServices(IServiceCollection services)
3131
});
3232
}
3333

34-
public void ConfigureRetailServices(IServiceCollection services)
34+
public void ConfigureRetailServices(IServiceCollection services)
3535
{
3636
services.AddOptions();
3737
services.Configure<FakeOptions>(o =>
@@ -94,8 +94,8 @@ public IServiceProvider ConfigureProviderArgsServices(IApplicationBuilder me)
9494
return services.BuildServiceProvider();
9595
}
9696

97-
public virtual void Configure(IApplicationBuilder builder)
98-
{
99-
}
100-
}
97+
public virtual void Configure(IApplicationBuilder builder)
98+
{
99+
}
100+
}
101101
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using Microsoft.AspNet.Builder;
5+
using Microsoft.Framework.DependencyInjection;
6+
using Microsoft.Framework.DependencyInjection.Fallback;
7+
using Microsoft.Framework.OptionsModel;
8+
using System;
9+
10+
namespace Microsoft.AspNet.Hosting.Fakes
11+
{
12+
public class StartupBase
13+
{
14+
public StartupBase()
15+
{
16+
}
17+
18+
public void ConfigureBaseClassServices(IServiceCollection services)
19+
{
20+
services.AddOptions();
21+
services.Configure<FakeOptions>(o =>
22+
{
23+
o.Configured = true;
24+
o.Environment = "BaseClass";
25+
});
26+
}
27+
28+
}
29+
}

test/Microsoft.AspNet.Hosting.Tests/StartupManagerTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ public void StartupClassMayHaveHostingServicesInjected()
4242
[InlineData("StaticProvider")]
4343
[InlineData("Provider")]
4444
[InlineData("ProviderArgs")]
45-
public void StartupClassAddsConfigureServicesToApplicationServices(string environment)
45+
[InlineData("BaseClass")]
46+
public void StartupClassAddsConfigureServicesToApplicationServices(string environment)
4647
{
4748
var services = HostingServices.Create().BuildServiceProvider();
4849
var manager = services.GetRequiredService<IStartupManager>();

0 commit comments

Comments
 (0)