#212 Check for duplicate Configure or ConfigureServices methods.
This commit is contained in:
parent
bf6e293bfe
commit
00b5cdc4a2
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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<IFakeStartupCallback>(this);
|
||||
var services = serviceCollection.BuildServiceProvider();
|
||||
|
||||
var diagnosticMessages = new List<string>();
|
||||
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<InvalidOperationException>(() => 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<IFakeStartupCallback>(this);
|
||||
var services = serviceCollection.BuildServiceProvider();
|
||||
|
||||
var diagnosticMessages = new List<string>();
|
||||
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<InvalidOperationException>(() => loader.LoadMethods(type, diagnosticMessages));
|
||||
Assert.Equal("Having multiple overloads of method 'ConfigureServices' is not supported.", ex.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StartupClassCanHandleConfigureServicesThatReturnsNull()
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in New Issue