diff --git a/shared/Microsoft.AspNetCore.Hosting.WebHostBuilderFactory.Sources/WebHostFactoryResolver.cs b/shared/Microsoft.AspNetCore.Hosting.WebHostBuilderFactory.Sources/WebHostFactoryResolver.cs index 496bb6ddcc..916b15e020 100644 --- a/shared/Microsoft.AspNetCore.Hosting.WebHostBuilderFactory.Sources/WebHostFactoryResolver.cs +++ b/shared/Microsoft.AspNetCore.Hosting.WebHostBuilderFactory.Sources/WebHostFactoryResolver.cs @@ -19,8 +19,11 @@ namespace Microsoft.AspNetCore.Hosting.WebHostBuilderFactory return FactoryResolutionResult.NoEntryPoint(); } - var factory = programType?.GetTypeInfo().GetDeclaredMethod(CreateWebHostBuilder); - if (factory == null) + var factory = programType.GetTypeInfo().GetDeclaredMethod(CreateWebHostBuilder); + if (factory == null || + !typeof(TWebhostBuilder).IsAssignableFrom(factory.ReturnType) || + factory.GetParameters().Length != 1 || + !typeof(string []).Equals(factory.GetParameters()[0].ParameterType)) { return FactoryResolutionResult.NoCreateWebHostBuilder(programType); } @@ -39,8 +42,11 @@ namespace Microsoft.AspNetCore.Hosting.WebHostBuilderFactory return findResult; case FactoryResolutionResultKind.Success: case FactoryResolutionResultKind.NoCreateWebHostBuilder: - var buildWebHostMethod = findResult.ProgramType.GetTypeInfo().GetDeclaredMethod("BuildWebHost"); - if (buildWebHostMethod == null) + var buildWebHostMethod = findResult.ProgramType.GetTypeInfo().GetDeclaredMethod(BuildWebHost); + if (buildWebHostMethod == null || + !typeof(TWebhost).IsAssignableFrom(buildWebHostMethod.ReturnType) || + buildWebHostMethod.GetParameters().Length != 1 || + !typeof(string[]).Equals(buildWebHostMethod.GetParameters()[0].ParameterType)) { if (findResult.ResultKind == FactoryResolutionResultKind.Success) { diff --git a/test/Microsoft.AspNetCore.Hosting.WebHostBuilderFactory.Tests/Microsoft.AspNetCore.Hosting.WebHostBuilderFactory.Tests.csproj b/test/Microsoft.AspNetCore.Hosting.WebHostBuilderFactory.Tests/Microsoft.AspNetCore.Hosting.WebHostBuilderFactory.Tests.csproj index 54dc903f67..af6ea3865b 100644 --- a/test/Microsoft.AspNetCore.Hosting.WebHostBuilderFactory.Tests/Microsoft.AspNetCore.Hosting.WebHostBuilderFactory.Tests.csproj +++ b/test/Microsoft.AspNetCore.Hosting.WebHostBuilderFactory.Tests/Microsoft.AspNetCore.Hosting.WebHostBuilderFactory.Tests.csproj @@ -12,6 +12,8 @@ + + diff --git a/test/Microsoft.AspNetCore.Hosting.WebHostBuilderFactory.Tests/WebHostFactoryResolverTests.cs b/test/Microsoft.AspNetCore.Hosting.WebHostBuilderFactory.Tests/WebHostFactoryResolverTests.cs index f6b758781c..f2732f75c4 100644 --- a/test/Microsoft.AspNetCore.Hosting.WebHostBuilderFactory.Tests/WebHostFactoryResolverTests.cs +++ b/test/Microsoft.AspNetCore.Hosting.WebHostBuilderFactory.Tests/WebHostFactoryResolverTests.cs @@ -12,7 +12,7 @@ namespace Microsoft.AspNetCore.Hosting.WebHostBuilderFactory.Tests public void CanFindWebHostBuilder_CreateWebHostBuilderPattern() { // Arrange & Act - var resolverResult = WebHostFactoryResolver.ResolveWebHostBuilderFactory(typeof(IStartupInjectionAssemblyName.Startup).Assembly); + var resolverResult = WebHostFactoryResolver.ResolveWebHostBuilderFactory(typeof(IStartupInjectionAssemblyName.Startup).Assembly); // Assert Assert.Equal(FactoryResolutionResultKind.Success, resolverResult.ResultKind); @@ -45,6 +45,30 @@ namespace Microsoft.AspNetCore.Hosting.WebHostBuilderFactory.Tests Assert.Null(resolverResult.WebHostFactory); } + [Fact] + public void CanNotFindWebHostBuilder_CreateWebHostBuilderIncorrectSignature() + { + // Arrange & Act + var resolverResult = WebHostFactoryResolver.ResolveWebHostBuilderFactory(typeof(CreateWebHostBuilderInvalidSignature.Startup).Assembly); + + // Assert + Assert.Equal(FactoryResolutionResultKind.NoCreateWebHostBuilder, resolverResult.ResultKind); + Assert.Null(resolverResult.WebHostBuilderFactory); + Assert.Null(resolverResult.WebHostFactory); + } + + [Fact] + public void CanNotFindWebHost_BuildWebHostIncorrectSignature() + { + // Arrange & Act + var resolverResult = WebHostFactoryResolver.ResolveWebHostFactory(typeof(BuildWebHostInvalidSignature.Startup).Assembly); + + // Assert + Assert.Equal(FactoryResolutionResultKind.NoBuildWebHost, resolverResult.ResultKind); + Assert.Null(resolverResult.WebHostBuilderFactory); + Assert.Null(resolverResult.WebHostFactory); + } + [Fact] public void CanFindWebHost_BuildWebHostPattern() { diff --git a/test/TestAssets/BuildWebHostInvalidSignature/BuildWebHostInvalidSignature.csproj b/test/TestAssets/BuildWebHostInvalidSignature/BuildWebHostInvalidSignature.csproj new file mode 100644 index 0000000000..25a648a366 --- /dev/null +++ b/test/TestAssets/BuildWebHostInvalidSignature/BuildWebHostInvalidSignature.csproj @@ -0,0 +1,17 @@ + + + + $(StandardTestAssetTfms) + Exe + + + + + + + + + + + + diff --git a/test/TestAssets/BuildWebHostInvalidSignature/Program.cs b/test/TestAssets/BuildWebHostInvalidSignature/Program.cs new file mode 100644 index 0000000000..b25cb703ad --- /dev/null +++ b/test/TestAssets/BuildWebHostInvalidSignature/Program.cs @@ -0,0 +1,16 @@ +// 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.AspNetCore.Hosting; + +namespace BuildWebHostInvalidSignature +{ + class Program + { + static void Main(string[] args) + { + } + + public static IWebHost BuildWebHost() => null; + } +} diff --git a/test/TestAssets/BuildWebHostInvalidSignature/Startup.cs b/test/TestAssets/BuildWebHostInvalidSignature/Startup.cs new file mode 100644 index 0000000000..0b52bc36b8 --- /dev/null +++ b/test/TestAssets/BuildWebHostInvalidSignature/Startup.cs @@ -0,0 +1,19 @@ +// 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.AspNetCore.Builder; +using Microsoft.Extensions.DependencyInjection; + +namespace BuildWebHostInvalidSignature +{ + public class Startup + { + public void ConfigureServices(IServiceCollection services) + { + } + + public void Configure(IApplicationBuilder builder) + { + } + } +} diff --git a/test/TestAssets/CreateWebHostBuilderInvalidSignature/CreateWebHostBuilderInvalidSignature.csproj b/test/TestAssets/CreateWebHostBuilderInvalidSignature/CreateWebHostBuilderInvalidSignature.csproj new file mode 100644 index 0000000000..25a648a366 --- /dev/null +++ b/test/TestAssets/CreateWebHostBuilderInvalidSignature/CreateWebHostBuilderInvalidSignature.csproj @@ -0,0 +1,17 @@ + + + + $(StandardTestAssetTfms) + Exe + + + + + + + + + + + + diff --git a/test/TestAssets/CreateWebHostBuilderInvalidSignature/Program.cs b/test/TestAssets/CreateWebHostBuilderInvalidSignature/Program.cs new file mode 100644 index 0000000000..9322836fe0 --- /dev/null +++ b/test/TestAssets/CreateWebHostBuilderInvalidSignature/Program.cs @@ -0,0 +1,16 @@ +// 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.AspNetCore.Hosting; + +namespace CreateWebHostBuilderInvalidSignature +{ + class Program + { + static void Main(string[] args) + { + } + + public static IWebHostBuilder CreateWebHostBuilder() => null; + } +} diff --git a/test/TestAssets/CreateWebHostBuilderInvalidSignature/Startup.cs b/test/TestAssets/CreateWebHostBuilderInvalidSignature/Startup.cs new file mode 100644 index 0000000000..2655a7e937 --- /dev/null +++ b/test/TestAssets/CreateWebHostBuilderInvalidSignature/Startup.cs @@ -0,0 +1,19 @@ +// 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.AspNetCore.Builder; +using Microsoft.Extensions.DependencyInjection; + +namespace CreateWebHostBuilderInvalidSignature +{ + public class Startup + { + public void ConfigureServices(IServiceCollection services) + { + } + + public void Configure(IApplicationBuilder builder) + { + } + } +}