From 85f3c841f1f99d8f527dab60db0492c06a3eb73a Mon Sep 17 00:00:00 2001 From: Javier Calvarro Nelson Date: Tue, 19 Sep 2017 21:45:05 -0700 Subject: [PATCH] Revert "Revert "Improvements to the testing package."" This reverts commit 7b28334c4184223f03028e5931a4fab53a8961b3. --- .../Internal/TestServiceRegistrations.cs | 33 ---- .../Internal/TestStartup.cs | 55 ------- .../MvcWebApplicationBuilder.cs | 145 ------------------ .../Properties/Resources.Designer.cs | 44 ++++++ .../Resources.resx | 123 +++++++++++++++ .../WebApplicationTestFixture.cs | 57 +++++-- .../BasicTests.cs | 10 ++ .../MvcEncodedTestFixtureOfT.cs | 8 +- .../Infrastructure/MvcSampleFixture.cs | 1 - .../Infrastructure/MvcTestFixture.cs | 12 +- .../MvcWebApplicationBuilderExtensions.cs | 6 +- .../Controllers/TestingController.cs | 21 +++ test/WebSites/BasicWebSite/Program.cs | 20 ++- 13 files changed, 263 insertions(+), 272 deletions(-) delete mode 100644 src/Microsoft.AspNetCore.Mvc.Testing/Internal/TestServiceRegistrations.cs delete mode 100644 src/Microsoft.AspNetCore.Mvc.Testing/Internal/TestStartup.cs delete mode 100644 src/Microsoft.AspNetCore.Mvc.Testing/MvcWebApplicationBuilder.cs create mode 100644 src/Microsoft.AspNetCore.Mvc.Testing/Properties/Resources.Designer.cs create mode 100644 src/Microsoft.AspNetCore.Mvc.Testing/Resources.resx create mode 100644 test/WebSites/BasicWebSite/Controllers/TestingController.cs diff --git a/src/Microsoft.AspNetCore.Mvc.Testing/Internal/TestServiceRegistrations.cs b/src/Microsoft.AspNetCore.Mvc.Testing/Internal/TestServiceRegistrations.cs deleted file mode 100644 index 018c55bba4..0000000000 --- a/src/Microsoft.AspNetCore.Mvc.Testing/Internal/TestServiceRegistrations.cs +++ /dev/null @@ -1,33 +0,0 @@ -// 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 System; -using System.Collections.Generic; -using Microsoft.Extensions.DependencyInjection; - -namespace Microsoft.AspNetCore.Mvc.Testing.Internal -{ - /// - /// Helper class to orchestrate service registrations in . - /// - public class TestServiceRegistrations - { - public IList> Before { get; set; } = new List>(); - public IList> After { get; set; } = new List>(); - - public void ConfigureServices(IServiceCollection services, Action startupConfigureServices) - { - foreach (var config in Before) - { - config(services); - } - - startupConfigureServices(); - - foreach (var config in After) - { - config(services); - } - } - } -} diff --git a/src/Microsoft.AspNetCore.Mvc.Testing/Internal/TestStartup.cs b/src/Microsoft.AspNetCore.Mvc.Testing/Internal/TestStartup.cs deleted file mode 100644 index 2e9665d4e7..0000000000 --- a/src/Microsoft.AspNetCore.Mvc.Testing/Internal/TestStartup.cs +++ /dev/null @@ -1,55 +0,0 @@ -// 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 System; -using System.Linq; -using Microsoft.AspNetCore.Builder; -using Microsoft.Extensions.DependencyInjection; - -namespace Microsoft.AspNetCore.Mvc.Testing.Internal -{ - /// - /// Fake startup class used in functional tests to decorate the registration of - /// ConfigureServices. - /// - /// The startup class of your application. - public class TestStartup where TStartup : class - { - private readonly IServiceProvider _serviceProvider; - private readonly TestServiceRegistrations _registrations; - private readonly TStartup _instance; - - public TestStartup(IServiceProvider serviceProvider, TestServiceRegistrations registrations) - { - _serviceProvider = serviceProvider; - _registrations = registrations; - _instance = (TStartup)ActivatorUtilities.CreateInstance(serviceProvider, typeof(TStartup)); - } - - public void ConfigureServices(IServiceCollection services) - { - var configureServices = _instance.GetType().GetMethod(nameof(ConfigureServices)); - var parameters = Enumerable.Repeat(services, 1) - .Concat(configureServices - .GetParameters() - .Skip(1) - .Select(p => ActivatorUtilities.GetServiceOrCreateInstance(_serviceProvider, p.ParameterType))) - .ToArray(); - - _registrations.ConfigureServices(services, () => configureServices.Invoke(_instance, parameters)); - } - - public void Configure(IApplicationBuilder applicationBuilder) - { - var configure = _instance.GetType().GetMethod(nameof(Configure)); - var parameters = Enumerable.Repeat(applicationBuilder, 1) - .Concat(configure - .GetParameters() - .Skip(1) - .Select(p => ActivatorUtilities.GetServiceOrCreateInstance(_serviceProvider, p.ParameterType))) - .ToArray(); - - configure.Invoke(_instance, parameters); - } - } -} diff --git a/src/Microsoft.AspNetCore.Mvc.Testing/MvcWebApplicationBuilder.cs b/src/Microsoft.AspNetCore.Mvc.Testing/MvcWebApplicationBuilder.cs deleted file mode 100644 index e216b4115c..0000000000 --- a/src/Microsoft.AspNetCore.Mvc.Testing/MvcWebApplicationBuilder.cs +++ /dev/null @@ -1,145 +0,0 @@ -// 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 System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Reflection; -using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.Mvc.ApplicationParts; -using Microsoft.AspNetCore.Mvc.Internal; -using Microsoft.AspNetCore.Mvc.Testing.Internal; -using Microsoft.AspNetCore.TestHost; -using Microsoft.Extensions.DependencyInjection; - -namespace Microsoft.AspNetCore.Mvc.Testing -{ - /// - /// Builder API for bootstraping an MVC application for functional tests. - /// - /// The application startup class. - public class MvcWebApplicationBuilder where TStartup : class - { - public string ContentRoot { get; set; } - public IList> ConfigureServicesBeforeStartup { get; set; } = new List>(); - public IList> ConfigureServicesAfterStartup { get; set; } = new List>(); - public List ApplicationAssemblies { get; set; } = new List(); - - /// - /// Configures services before TStartup.ConfigureServices runs. - /// - /// The to configure the services with. - /// An instance of this - public MvcWebApplicationBuilder ConfigureBeforeStartup(Action configure) - { - ConfigureServicesBeforeStartup.Add(configure); - return this; - } - - /// - /// Configures services after TStartup.ConfigureServices runs. - /// - /// The to configure the services with. - /// An instance of this - public MvcWebApplicationBuilder ConfigureAfterStartup(Action configure) - { - ConfigureServicesAfterStartup.Add(configure); - return this; - } - - /// - /// Configures to include the default set - /// of provided by . - /// - /// An instance of this - public MvcWebApplicationBuilder UseApplicationAssemblies() - { - var depsFileName = $"{typeof(TStartup).Assembly.GetName().Name}.deps.json"; - var depsFile = new FileInfo(Path.Combine(AppContext.BaseDirectory, depsFileName)); - if (!depsFile.Exists) - { - throw new InvalidOperationException($"Can't find'{depsFile.FullName}'. This file is required for functional tests " + - "to run properly. There should be a copy of the file on your source project bin folder. If thats not the " + - "case, make sure that the property PreserveCompilationContext is set to true on your project file. E.g" + - "'true'." + - $"For functional tests to work they need to either run from the build output folder or the {Path.GetFileName(depsFile.FullName)} " + - $"file from your application's output directory must be copied" + - "to the folder where the tests are running on. A common cause for this error is having shadow copying enabled when the " + - "tests run."); - } - - ApplicationAssemblies.AddRange(DefaultAssemblyPartDiscoveryProvider - .DiscoverAssemblyParts(typeof(TStartup).Assembly.GetName().Name) - .Select(s => ((AssemblyPart)s).Assembly) - .ToList()); - - return this; - } - - /// - /// Configures the application content root. - /// - /// The glob pattern to use for finding the solution. - /// The relative path to the content root from the solution file. - /// An instance of this - public MvcWebApplicationBuilder UseSolutionRelativeContentRoot( - string solutionRelativePath, - string solutionName = "*.sln") - { - if (solutionRelativePath == null) - { - throw new ArgumentNullException(nameof(solutionRelativePath)); - } - - var applicationBasePath = AppContext.BaseDirectory; - - var directoryInfo = new DirectoryInfo(applicationBasePath); - do - { - var solutionPath = Directory.EnumerateFiles(directoryInfo.FullName, solutionName).FirstOrDefault(); - if (solutionPath != null) - { - ContentRoot = Path.GetFullPath(Path.Combine(directoryInfo.FullName, solutionRelativePath)); - return this; - } - - directoryInfo = directoryInfo.Parent; - } - while (directoryInfo.Parent != null); - - throw new Exception($"Solution root could not be located using application root {applicationBasePath}."); - } - - public TestServer Build() - { - var builder = new WebHostBuilder() - .UseStartup>() - // This is necessary so that IHostingEnvironment.ApplicationName has the right - // value and libraries depending on it (to load the dependency context, for example) - // work properly. - .UseSetting(WebHostDefaults.ApplicationKey, typeof(TStartup).Assembly.GetName().Name) - .UseContentRoot(ContentRoot) - .ConfigureServices(InitializeServices); - - return new TestServer(builder); - } - - protected virtual void InitializeServices(IServiceCollection services) - { - // Inject a custom application part manager. Overrides AddMvcCore() because that uses TryAdd(). - var manager = new ApplicationPartManager(); - foreach (var assembly in ApplicationAssemblies) - { - manager.ApplicationParts.Add(new AssemblyPart(assembly)); - } - - services.AddSingleton(manager); - services.AddSingleton(new TestServiceRegistrations - { - Before = ConfigureServicesBeforeStartup, - After = ConfigureServicesAfterStartup - }); - } - } -} diff --git a/src/Microsoft.AspNetCore.Mvc.Testing/Properties/Resources.Designer.cs b/src/Microsoft.AspNetCore.Mvc.Testing/Properties/Resources.Designer.cs new file mode 100644 index 0000000000..41de4ef679 --- /dev/null +++ b/src/Microsoft.AspNetCore.Mvc.Testing/Properties/Resources.Designer.cs @@ -0,0 +1,44 @@ +// +namespace Microsoft.AspNetCore.Mvc.Testing +{ + using System.Globalization; + using System.Reflection; + using System.Resources; + + internal static class Resources + { + private static readonly ResourceManager _resourceManager + = new ResourceManager("Microsoft.AspNetCore.Mvc.Testing.Resources", typeof(Resources).GetTypeInfo().Assembly); + + /// + /// Can't find'{0}'. This file is required for functional tests to run properly. There should be a copy of the file on your source project bin folder. If that is not the case, make sure that the property PreserveCompilationContext is set to true on your project file. E.g '<PreserveCompilationContext>true</PreserveCompilationContext>'. For functional tests to work they need to either run from the build output folder or the {1} file from your application's output directory must be copied to the folder where the tests are running on. A common cause for this error is having shadow copying enabled when the tests run. + /// + internal static string MissingDepsFile + { + get => GetString("MissingDepsFile"); + } + + /// + /// Can't find'{0}'. This file is required for functional tests to run properly. There should be a copy of the file on your source project bin folder. If that is not the case, make sure that the property PreserveCompilationContext is set to true on your project file. E.g '<PreserveCompilationContext>true</PreserveCompilationContext>'. For functional tests to work they need to either run from the build output folder or the {1} file from your application's output directory must be copied to the folder where the tests are running on. A common cause for this error is having shadow copying enabled when the tests run. + /// + internal static string FormatMissingDepsFile(object p0, object p1) + => string.Format(CultureInfo.CurrentCulture, GetString("MissingDepsFile"), p0, p1); + + private static string GetString(string name, params string[] formatterNames) + { + var value = _resourceManager.GetString(name); + + System.Diagnostics.Debug.Assert(value != null); + + if (formatterNames != null) + { + for (var i = 0; i < formatterNames.Length; i++) + { + value = value.Replace("{" + formatterNames[i] + "}", "{" + i + "}"); + } + } + + return value; + } + } +} diff --git a/src/Microsoft.AspNetCore.Mvc.Testing/Resources.resx b/src/Microsoft.AspNetCore.Mvc.Testing/Resources.resx new file mode 100644 index 0000000000..3ba53d0317 --- /dev/null +++ b/src/Microsoft.AspNetCore.Mvc.Testing/Resources.resx @@ -0,0 +1,123 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Can't find'{0}'. This file is required for functional tests to run properly. There should be a copy of the file on your source project bin folder. If that is not the case, make sure that the property PreserveCompilationContext is set to true on your project file. E.g '<PreserveCompilationContext>true</PreserveCompilationContext>'. For functional tests to work they need to either run from the build output folder or the {1} file from your application's output directory must be copied to the folder where the tests are running on. A common cause for this error is having shadow copying enabled when the tests run. + + \ No newline at end of file diff --git a/src/Microsoft.AspNetCore.Mvc.Testing/WebApplicationTestFixture.cs b/src/Microsoft.AspNetCore.Mvc.Testing/WebApplicationTestFixture.cs index a5a0697677..d66d9e60bf 100644 --- a/src/Microsoft.AspNetCore.Mvc.Testing/WebApplicationTestFixture.cs +++ b/src/Microsoft.AspNetCore.Mvc.Testing/WebApplicationTestFixture.cs @@ -1,9 +1,10 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// 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 System; using System.IO; using System.Net.Http; +using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.TestHost; namespace Microsoft.AspNetCore.Mvc.Testing @@ -23,7 +24,7 @@ namespace Microsoft.AspNetCore.Mvc.Testing /// /// /// This constructor will infer the application root directive by searching for a solution file (*.sln) and then - /// appending the path src/{AssemblyName} to the solution directory.The application root directory will be + /// appending the path{AssemblyName} to the solution directory.The application root directory will be /// used to discover views and content files. /// /// @@ -33,7 +34,7 @@ namespace Microsoft.AspNetCore.Mvc.Testing /// /// public WebApplicationTestFixture() - : this(Path.Combine("src", typeof(TStartup).Assembly.GetName().Name)) + : this(typeof(TStartup).Assembly.GetName().Name) { } @@ -56,7 +57,7 @@ namespace Microsoft.AspNetCore.Mvc.Testing /// The path to the project folder relative to the solution file of your /// application. The folder of the first .sln file found traversing up the folder hierarchy from the test execution /// folder is considered as the base path. - protected WebApplicationTestFixture(string solutionRelativePath) + public WebApplicationTestFixture(string solutionRelativePath) : this("*.sln", solutionRelativePath) { } @@ -82,35 +83,59 @@ namespace Microsoft.AspNetCore.Mvc.Testing /// The path to the project folder relative to the solution file of your /// application. The folder of the first sln file that matches the /// found traversing up the folder hierarchy from the test execution folder is considered as the base path. - protected WebApplicationTestFixture(string solutionSearchPattern, string solutionRelativePath) + public WebApplicationTestFixture(string solutionSearchPattern, string solutionRelativePath) { - var builder = new MvcWebApplicationBuilder() - .UseSolutionRelativeContentRoot(solutionRelativePath) - .UseApplicationAssemblies(); + EnsureDepsFile(); - ConfigureApplication(builder); + var builder = CreateWebHostBuilder(); + builder + .UseStartup() + .UseSolutionRelativeContentRoot(solutionRelativePath); + + ConfigureWebHost(builder); _server = CreateServer(builder); Client = _server.CreateClient(); Client.BaseAddress = new Uri("http://localhost"); } + private void EnsureDepsFile() + { + var depsFileName = $"{typeof(TStartup).Assembly.GetName().Name}.deps.json"; + var depsFile = new FileInfo(Path.Combine(AppContext.BaseDirectory, depsFileName)); + if (!depsFile.Exists) + { + throw new InvalidOperationException(Resources.FormatMissingDepsFile( + depsFile.FullName, + Path.GetFileName(depsFile.FullName))); + } + } + + /// + /// Creates a used to setup . + /// + /// The default implementation of this method looks for a public static IWebHostBuilder CreateDefaultBuilder(string[] args) + /// method defined on the entry point of the assembly of and invokes it passing an empty string + /// array as arguments. In case this method can't be found, + /// + /// + /// A instance. + protected virtual IWebHostBuilder CreateWebHostBuilder() => + WebHostBuilderFactory.CreateFromTypesAssemblyEntryPoint(Array.Empty()) ?? new WebHostBuilder(); + /// /// Creates the with the bootstrapped application in . /// - /// The used to + /// The used to /// create the server. /// The with the bootstrapped application. - protected virtual TestServer CreateServer(MvcWebApplicationBuilder builder) - { - return builder.Build(); - } + protected virtual TestServer CreateServer(IWebHostBuilder builder) => new TestServer(builder); /// /// Gives a fixture an opportunity to configure the application before it gets built. /// - /// The for the application. - protected virtual void ConfigureApplication(MvcWebApplicationBuilder builder) + /// The for the application. + protected virtual void ConfigureWebHost(IWebHostBuilder builder) { } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/BasicTests.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/BasicTests.cs index 0a6009a79f..5aefa2b39b 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/BasicTests.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/BasicTests.cs @@ -460,5 +460,15 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests var result = JsonConvert.DeserializeObject(response); Assert.Equal(2, result.Length); } + + [Fact] + public async Task TestingInfrastructure_InvokesCreateDefaultBuilder() + { + // Act + var response = await Client.GetStringAsync("Testing/Builder"); + + // Assert + Assert.Equal("true", response); + } } } diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/Infrastructure/MvcEncodedTestFixtureOfT.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/Infrastructure/MvcEncodedTestFixtureOfT.cs index 396842ce1b..b978f8c818 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/Infrastructure/MvcEncodedTestFixtureOfT.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/Infrastructure/MvcEncodedTestFixtureOfT.cs @@ -2,7 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.Text.Encodings.Web; -using Microsoft.AspNetCore.Mvc.Testing; +using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.WebEncoders.Testing; @@ -11,10 +11,10 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests public class MvcEncodedTestFixture : MvcTestFixture where TStartup : class { - protected override void ConfigureApplication(MvcWebApplicationBuilder builder) + protected override void ConfigureWebHost(IWebHostBuilder builder) { - base.ConfigureApplication(builder); - builder.ConfigureBeforeStartup(services => + base.ConfigureWebHost(builder); + builder.ConfigureServices(services => { services.TryAddTransient(); services.TryAddTransient(); diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/Infrastructure/MvcSampleFixture.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/Infrastructure/MvcSampleFixture.cs index 17fce4b085..53ebde8045 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/Infrastructure/MvcSampleFixture.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/Infrastructure/MvcSampleFixture.cs @@ -2,7 +2,6 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.IO; -using System.Reflection; namespace Microsoft.AspNetCore.Mvc.FunctionalTests { diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/Infrastructure/MvcTestFixture.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/Infrastructure/MvcTestFixture.cs index 36470f9d2f..f1c9ed99e0 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/Infrastructure/MvcTestFixture.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/Infrastructure/MvcTestFixture.cs @@ -3,7 +3,7 @@ using System.Globalization; using System.IO; -using System.Reflection; +using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.AspNetCore.TestHost; @@ -22,14 +22,10 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests { } - protected override void ConfigureApplication(MvcWebApplicationBuilder builder) - { - builder.UseRequestCulture("en-GB", "en-US"); - builder.ApplicationAssemblies.Clear(); - builder.ApplicationAssemblies.Add(typeof(TStartup).GetTypeInfo().Assembly); - } + protected override void ConfigureWebHost(IWebHostBuilder builder) => + builder.UseRequestCulture("en-GB", "en-US"); - protected override TestServer CreateServer(MvcWebApplicationBuilder builder) + protected override TestServer CreateServer(IWebHostBuilder builder) { var originalCulture = CultureInfo.CurrentCulture; var originalUICulture = CultureInfo.CurrentUICulture; diff --git a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/Infrastructure/MvcWebApplicationBuilderExtensions.cs b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/Infrastructure/MvcWebApplicationBuilderExtensions.cs index 6a39eb5785..d3da7ba515 100644 --- a/test/Microsoft.AspNetCore.Mvc.FunctionalTests/Infrastructure/MvcWebApplicationBuilderExtensions.cs +++ b/test/Microsoft.AspNetCore.Mvc.FunctionalTests/Infrastructure/MvcWebApplicationBuilderExtensions.cs @@ -19,8 +19,8 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests /// /// The culture to use when processing . /// The UI culture to use when processing . - /// An instance of this - public static MvcWebApplicationBuilder UseRequestCulture(this MvcWebApplicationBuilder builder, string culture, string uiCulture) + /// An instance of this + public static IWebHostBuilder UseRequestCulture(this IWebHostBuilder builder, string culture, string uiCulture) where TStartup : class { if (culture == null) @@ -33,7 +33,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests throw new ArgumentNullException(nameof(uiCulture)); } - builder.ConfigureBeforeStartup(services => + builder.ConfigureServices(services => { services.TryAddSingleton(new TestCulture { diff --git a/test/WebSites/BasicWebSite/Controllers/TestingController.cs b/test/WebSites/BasicWebSite/Controllers/TestingController.cs new file mode 100644 index 0000000000..37256b3bb9 --- /dev/null +++ b/test/WebSites/BasicWebSite/Controllers/TestingController.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; + +namespace BasicWebSite.Controllers +{ + public class TestingController + { + public TestingController(TestService service) + { + Service = service; + } + + public TestService Service { get; } + + [HttpGet("Testing/Builder")] + public string Get() => Service.Message; + } +} diff --git a/test/WebSites/BasicWebSite/Program.cs b/test/WebSites/BasicWebSite/Program.cs index c2a444d9d9..2de761fdc5 100644 --- a/test/WebSites/BasicWebSite/Program.cs +++ b/test/WebSites/BasicWebSite/Program.cs @@ -3,21 +3,27 @@ using System.IO; using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.DependencyInjection; namespace BasicWebSite { public class Program { - public static void Main(string[] args) - { - var host = new WebHostBuilder() + public static void Main(string[] args) => CreateWebHostBuilder(args).Build().Run(); + + // Do not change. This is the pattern our test infrastructure uses to initialize a IWebHostBuilder from + // a users app. + public static IWebHostBuilder CreateWebHostBuilder(string[] args) => + new WebHostBuilder() .UseContentRoot(Directory.GetCurrentDirectory()) .UseStartup() + .ConfigureServices(s => s.AddSingleton(new TestService { Message = "true" })) .UseKestrel() - .UseIISIntegration() - .Build(); + .UseIISIntegration(); + } - host.Run(); - } + public class TestService + { + public string Message { get; set; } } }