From fe3675a9e06f5a3ff1eed9c8aeb8108f88e90d10 Mon Sep 17 00:00:00 2001 From: dtkujawski Date: Thu, 12 Feb 2015 17:40:14 -0600 Subject: [PATCH] Ability to derive Startup from a base class and have "Configure" and "ConfigureServices" be invoked from the base class when present. --- .../Startup/StartupLoader.cs | 20 ++++++------- .../Fakes/Startup.cs | 12 ++++---- .../Fakes/StartupBase.cs | 29 +++++++++++++++++++ .../StartupManagerTests.cs | 3 +- 4 files changed, 47 insertions(+), 17 deletions(-) create mode 100644 test/Microsoft.AspNet.Hosting.Tests/Fakes/StartupBase.cs diff --git a/src/Microsoft.AspNet.Hosting/Startup/StartupLoader.cs b/src/Microsoft.AspNet.Hosting/Startup/StartupLoader.cs index a7e85283bf..24770cd502 100644 --- a/src/Microsoft.AspNet.Hosting/Startup/StartupLoader.cs +++ b/src/Microsoft.AspNet.Hosting/Startup/StartupLoader.cs @@ -29,18 +29,18 @@ namespace Microsoft.AspNet.Hosting.Startup { var methodNameWithEnv = string.Format(CultureInfo.InvariantCulture, methodName, environmentName); var methodNameWithNoEnv = string.Format(CultureInfo.InvariantCulture, methodName, ""); - var methodInfo = startupType.GetTypeInfo().GetDeclaredMethod(methodNameWithEnv) - ?? startupType.GetTypeInfo().GetDeclaredMethod(methodNameWithNoEnv); - if (methodInfo == null) + var methodInfo = startupType.GetMethod(methodNameWithEnv, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static) + ?? startupType.GetMethod(methodNameWithNoEnv, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static); + if (methodInfo == null) { if (required) - { - throw new Exception(string.Format("TODO: {0} or {1} method not found", - methodNameWithEnv, - methodNameWithNoEnv)); + { + throw new Exception(string.Format("TODO: {0} or {1} method not found", + methodNameWithEnv, + methodNameWithNoEnv)); - } - return null; + } + return null; } if (returnType != null && methodInfo.ReturnType != returnType) { @@ -160,7 +160,7 @@ namespace Microsoft.AspNet.Hosting.Startup } else { - // void ConfigureServices(IServiceCollection) + // void ConfigureServices(IServiceCollection) Invoke(servicesMethod, instance, builder, services); if (builder != null) { diff --git a/test/Microsoft.AspNet.Hosting.Tests/Fakes/Startup.cs b/test/Microsoft.AspNet.Hosting.Tests/Fakes/Startup.cs index e657841566..54ef6de669 100644 --- a/test/Microsoft.AspNet.Hosting.Tests/Fakes/Startup.cs +++ b/test/Microsoft.AspNet.Hosting.Tests/Fakes/Startup.cs @@ -9,7 +9,7 @@ using System; namespace Microsoft.AspNet.Hosting.Fakes { - public class Startup + public class Startup : StartupBase { public Startup() { @@ -31,7 +31,7 @@ namespace Microsoft.AspNet.Hosting.Fakes }); } - public void ConfigureRetailServices(IServiceCollection services) + public void ConfigureRetailServices(IServiceCollection services) { services.AddOptions(); services.Configure(o => @@ -94,8 +94,8 @@ namespace Microsoft.AspNet.Hosting.Fakes return services.BuildServiceProvider(); } - public virtual void Configure(IApplicationBuilder builder) - { - } - } + public virtual void Configure(IApplicationBuilder builder) + { + } + } } \ No newline at end of file diff --git a/test/Microsoft.AspNet.Hosting.Tests/Fakes/StartupBase.cs b/test/Microsoft.AspNet.Hosting.Tests/Fakes/StartupBase.cs new file mode 100644 index 0000000000..f1ee427a87 --- /dev/null +++ b/test/Microsoft.AspNet.Hosting.Tests/Fakes/StartupBase.cs @@ -0,0 +1,29 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using Microsoft.AspNet.Builder; +using Microsoft.Framework.DependencyInjection; +using Microsoft.Framework.DependencyInjection.Fallback; +using Microsoft.Framework.OptionsModel; +using System; + +namespace Microsoft.AspNet.Hosting.Fakes +{ + public class StartupBase + { + public StartupBase() + { + } + + public void ConfigureBaseClassServices(IServiceCollection services) + { + services.AddOptions(); + services.Configure(o => + { + o.Configured = true; + o.Environment = "BaseClass"; + }); + } + + } +} \ 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 f3323b978a..46758b92bc 100644 --- a/test/Microsoft.AspNet.Hosting.Tests/StartupManagerTests.cs +++ b/test/Microsoft.AspNet.Hosting.Tests/StartupManagerTests.cs @@ -42,7 +42,8 @@ namespace Microsoft.AspNet.Hosting.Tests [InlineData("StaticProvider")] [InlineData("Provider")] [InlineData("ProviderArgs")] - public void StartupClassAddsConfigureServicesToApplicationServices(string environment) + [InlineData("BaseClass")] + public void StartupClassAddsConfigureServicesToApplicationServices(string environment) { var services = HostingServices.Create().BuildServiceProvider(); var manager = services.GetRequiredService();