diff --git a/src/Microsoft.AspNet.Hosting/Startup/StartupLoader.cs b/src/Microsoft.AspNet.Hosting/Startup/StartupLoader.cs index 383d0d8357..28d716ffa4 100644 --- a/src/Microsoft.AspNet.Hosting/Startup/StartupLoader.cs +++ b/src/Microsoft.AspNet.Hosting/Startup/StartupLoader.cs @@ -105,8 +105,23 @@ namespace Microsoft.AspNet.Hosting.Startup { var methodNameWithEnv = string.Format(CultureInfo.InvariantCulture, methodName, environmentName); var methodNameWithNoEnv = string.Format(CultureInfo.InvariantCulture, methodName, ""); - var methodInfo = startupType.GetMethod(methodNameWithEnv, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static) - ?? startupType.GetMethod(methodNameWithNoEnv, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static); + + var methods = startupType.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static); + var selectedMethods = methods.Where(method => method.Name.Equals(methodNameWithEnv)).ToList(); + if (selectedMethods.Count > 1) + { + throw new InvalidOperationException(string.Format("Having multiple overloads of method '{0}' is not supported.", methodNameWithEnv)); + } + if (selectedMethods.Count == 0) + { + selectedMethods = methods.Where(method => method.Name.Equals(methodNameWithNoEnv)).ToList(); + if (selectedMethods.Count > 1) + { + throw new InvalidOperationException(string.Format("Having multiple overloads of method '{0}' is not supported.", methodNameWithNoEnv)); + } + } + + var methodInfo = selectedMethods.FirstOrDefault(); if (methodInfo == null) { if (required) diff --git a/test/Microsoft.AspNet.Hosting.Tests/Fakes/StartupTwoConfigureServices.cs b/test/Microsoft.AspNet.Hosting.Tests/Fakes/StartupTwoConfigureServices.cs new file mode 100644 index 0000000000..0442c3785c --- /dev/null +++ b/test/Microsoft.AspNet.Hosting.Tests/Fakes/StartupTwoConfigureServices.cs @@ -0,0 +1,30 @@ +// Copyright (c) .NET Foundation. 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; + +namespace Microsoft.AspNet.Hosting.Fakes +{ + public class StartupTwoConfigureServices + { + public StartupTwoConfigureServices() + { + } + + public void ConfigureServices(IServiceCollection services) + { + + } + + public void ConfigureServices(IServiceCollection services, object service) + { + + } + + public void Configure(IApplicationBuilder builder) + { + + } + } +} \ No newline at end of file diff --git a/test/Microsoft.AspNet.Hosting.Tests/Fakes/StartupTwoConfigures.cs b/test/Microsoft.AspNet.Hosting.Tests/Fakes/StartupTwoConfigures.cs new file mode 100644 index 0000000000..a97d679ac1 --- /dev/null +++ b/test/Microsoft.AspNet.Hosting.Tests/Fakes/StartupTwoConfigures.cs @@ -0,0 +1,24 @@ +// Copyright (c) .NET Foundation. 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; + +namespace Microsoft.AspNet.Hosting.Fakes +{ + public class StartupTwoConfigures + { + public StartupTwoConfigures() + { + } + + public void Configure(IApplicationBuilder builder) + { + + } + + public void Configure(IApplicationBuilder builder, object service) + { + + } + } +} \ 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 013f202f03..9cc385ae40 100644 --- a/test/Microsoft.AspNet.Hosting.Tests/StartupManagerTests.cs +++ b/test/Microsoft.AspNet.Hosting.Tests/StartupManagerTests.cs @@ -83,6 +83,38 @@ namespace Microsoft.AspNet.Hosting.Tests Assert.Equal("A method named 'ConfigureBoom' or 'Configure' in the type 'Microsoft.AspNet.Hosting.Fakes.StartupBoom' could not be found.", ex.Message); } + [Fact] + public void StartupWithTwoConfiguresThrows() + { + var serviceCollection = new ServiceCollection(); + serviceCollection.AddInstance(this); + var services = serviceCollection.BuildServiceProvider(); + + var diagnosticMessages = new List(); + var hostingEnv = new HostingEnvironment { EnvironmentName = "TwoConfigures" }; + var loader = new StartupLoader(services, hostingEnv); + var type = loader.FindStartupType("Microsoft.AspNet.Hosting.Tests", diagnosticMessages); + + var ex = Assert.Throws(() => loader.LoadMethods(type, diagnosticMessages)); + Assert.Equal("Having multiple overloads of method 'Configure' is not supported.", ex.Message); + } + + [Fact] + public void StartupWithTwoConfigureServicesThrows() + { + var serviceCollection = new ServiceCollection(); + serviceCollection.AddInstance(this); + var services = serviceCollection.BuildServiceProvider(); + + var diagnosticMessages = new List(); + var hostingEnv = new HostingEnvironment { EnvironmentName = "TwoConfigureServices" }; + var loader = new StartupLoader(services, hostingEnv); + var type = loader.FindStartupType("Microsoft.AspNet.Hosting.Tests", diagnosticMessages); + + var ex = Assert.Throws(() => loader.LoadMethods(type, diagnosticMessages)); + Assert.Equal("Having multiple overloads of method 'ConfigureServices' is not supported.", ex.Message); + } + [Fact] public void StartupClassCanHandleConfigureServicesThatReturnsNull() {