From 1717b97444ca5beccb5742ac9fb3fd76fbc19d76 Mon Sep 17 00:00:00 2001 From: "Chris Ross (ASP.NET)" Date: Tue, 24 Apr 2018 11:15:00 -0700 Subject: [PATCH 1/2] Run self-host tests with no-build. #1399 --- .../Common/DeploymentParameters.cs | 7 +++++++ .../Deployers/SelfHostDeployer.cs | 4 +--- .../IStartupInjectionAssemblyName/Program.cs | 11 +++++++---- .../IStartupInjectionAssemblyName/Startup.cs | 5 ----- 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/Microsoft.AspNetCore.Server.IntegrationTesting/Common/DeploymentParameters.cs b/src/Microsoft.AspNetCore.Server.IntegrationTesting/Common/DeploymentParameters.cs index 02b9c10cfa..3d824741c3 100644 --- a/src/Microsoft.AspNetCore.Server.IntegrationTesting/Common/DeploymentParameters.cs +++ b/src/Microsoft.AspNetCore.Server.IntegrationTesting/Common/DeploymentParameters.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Reflection; namespace Microsoft.AspNetCore.Server.IntegrationTesting { @@ -45,6 +46,12 @@ namespace Microsoft.AspNetCore.Server.IntegrationTesting ServerType = serverType; RuntimeFlavor = runtimeFlavor; EnvironmentVariables["ASPNETCORE_DETAILEDERRORS"] = "true"; + + var configAttribute = Assembly.GetCallingAssembly().GetCustomAttribute(); + if (configAttribute != null && !string.IsNullOrEmpty(configAttribute.Configuration)) + { + Configuration = configAttribute.Configuration; + } } public ServerType ServerType { get; } diff --git a/src/Microsoft.AspNetCore.Server.IntegrationTesting/Deployers/SelfHostDeployer.cs b/src/Microsoft.AspNetCore.Server.IntegrationTesting/Deployers/SelfHostDeployer.cs index 9f1c64dca1..99fb9b61d6 100644 --- a/src/Microsoft.AspNetCore.Server.IntegrationTesting/Deployers/SelfHostDeployer.cs +++ b/src/Microsoft.AspNetCore.Server.IntegrationTesting/Deployers/SelfHostDeployer.cs @@ -93,9 +93,7 @@ namespace Microsoft.AspNetCore.Server.IntegrationTesting var targetFramework = DeploymentParameters.TargetFramework ?? (DeploymentParameters.RuntimeFlavor == RuntimeFlavor.Clr ? "net461" : "netcoreapp2.0"); executableName = DotnetCommandName; - // Set VerifyMatchingImplicitPackageVersion to disable errors when Microsoft.NETCore.App's version is overridden externally - // This verification doesn't matter if we are skipping restore during tests. - executableArgs = $"run --no-restore --framework {targetFramework} /p:VerifyMatchingImplicitPackageVersion=false {DotnetArgumentSeparator}"; + executableArgs = $"run --no-build -c {DeploymentParameters.Configuration} --framework {targetFramework} {DotnetArgumentSeparator}"; } executableArgs += $" --server.urls {hintUrl} " diff --git a/test/TestAssets/IStartupInjectionAssemblyName/Program.cs b/test/TestAssets/IStartupInjectionAssemblyName/Program.cs index 15e8f4205c..405ec1fba0 100644 --- a/test/TestAssets/IStartupInjectionAssemblyName/Program.cs +++ b/test/TestAssets/IStartupInjectionAssemblyName/Program.cs @@ -13,13 +13,16 @@ namespace IStartupInjectionAssemblyName { public static void Main(string[] args) { - var server = new TestServer(CreateWebHostBuilder(args)); - - Task.Run(async () => Console.WriteLine(await server.CreateClient().GetStringAsync(""))).GetAwaiter().GetResult(); + var webHost = CreateWebHostBuilder(args).Build(); + var applicationName = webHost.Services.GetRequiredService().ApplicationName; + Console.WriteLine(applicationName); + Console.ReadKey(); } // Do not change the signature of this method. It's used for tests. private static IWebHostBuilder CreateWebHostBuilder(string [] args) => - new WebHostBuilder().SuppressStatusMessages(true).ConfigureServices(services => services.AddSingleton()); + new WebHostBuilder() + .SuppressStatusMessages(true) + .ConfigureServices(services => services.AddSingleton()); } } diff --git a/test/TestAssets/IStartupInjectionAssemblyName/Startup.cs b/test/TestAssets/IStartupInjectionAssemblyName/Startup.cs index c34254fba8..9f4e27223c 100644 --- a/test/TestAssets/IStartupInjectionAssemblyName/Startup.cs +++ b/test/TestAssets/IStartupInjectionAssemblyName/Startup.cs @@ -11,11 +11,6 @@ namespace IStartupInjectionAssemblyName { public void Configure(IApplicationBuilder app) { - var applicationName = app.ApplicationServices.GetRequiredService().ApplicationName; - app.Run(context => - { - return context.Response.WriteAsync(applicationName); - }); } public IServiceProvider ConfigureServices(IServiceCollection services) From 2c1376c95ff46b0870d323ee5c5b064af263062e Mon Sep 17 00:00:00 2001 From: "Chris Ross (ASP.NET)" Date: Tue, 1 May 2018 11:06:25 -0700 Subject: [PATCH 2/2] Add a AddHostedService extension #1402 --- .../ProgramExternallyControlled.cs | 4 ++-- .../GenericHostSample/ProgramFullControl.cs | 4 ++-- .../GenericHostSample/ProgramHelloWorld.cs | 4 ++-- .../ServiceBaseControlled.cs | 4 ++-- samples/GenericWebHost/WebHostExtensions.cs | 2 +- ...erviceCollectionHostedServiceExtensions.cs | 20 +++++++++++++++++++ .../WebHostTests.cs | 2 +- .../HostTests.cs | 4 ++-- 8 files changed, 32 insertions(+), 12 deletions(-) create mode 100644 src/Microsoft.Extensions.Hosting.Abstractions/ServiceCollectionHostedServiceExtensions.cs diff --git a/samples/GenericHostSample/ProgramExternallyControlled.cs b/samples/GenericHostSample/ProgramExternallyControlled.cs index 749442b161..7691b73ef2 100644 --- a/samples/GenericHostSample/ProgramExternallyControlled.cs +++ b/samples/GenericHostSample/ProgramExternallyControlled.cs @@ -25,8 +25,8 @@ namespace GenericHostSample }) .ConfigureServices((hostContext, services) => { - services.AddScoped(); - services.AddScoped(); + services.AddHostedService(); + services.AddHostedService(); }) .Build(); } diff --git a/samples/GenericHostSample/ProgramFullControl.cs b/samples/GenericHostSample/ProgramFullControl.cs index 59400077bb..e94477e003 100644 --- a/samples/GenericHostSample/ProgramFullControl.cs +++ b/samples/GenericHostSample/ProgramFullControl.cs @@ -23,8 +23,8 @@ namespace GenericHostSample }) .ConfigureServices((hostContext, services) => { - services.AddScoped(); - services.AddScoped(); + services.AddHostedService(); + services.AddHostedService(); }) .Build(); diff --git a/samples/GenericHostSample/ProgramHelloWorld.cs b/samples/GenericHostSample/ProgramHelloWorld.cs index a145a2534d..3145c40773 100644 --- a/samples/GenericHostSample/ProgramHelloWorld.cs +++ b/samples/GenericHostSample/ProgramHelloWorld.cs @@ -11,8 +11,8 @@ namespace GenericHostSample var builder = new HostBuilder() .ConfigureServices((hostContext, services) => { - services.AddScoped(); - services.AddScoped(); + services.AddHostedService(); + services.AddHostedService(); }); await builder.RunConsoleAsync(); diff --git a/samples/GenericHostSample/ServiceBaseControlled.cs b/samples/GenericHostSample/ServiceBaseControlled.cs index 431e061be5..24f63f992d 100644 --- a/samples/GenericHostSample/ServiceBaseControlled.cs +++ b/samples/GenericHostSample/ServiceBaseControlled.cs @@ -12,8 +12,8 @@ namespace GenericHostSample var builder = new HostBuilder() .ConfigureServices((hostContext, services) => { - services.AddScoped(); - services.AddScoped(); + services.AddHostedService(); + services.AddHostedService(); }); await builder.RunAsServiceAsync(); diff --git a/samples/GenericWebHost/WebHostExtensions.cs b/samples/GenericWebHost/WebHostExtensions.cs index 2226aba233..bf5567d81a 100644 --- a/samples/GenericWebHost/WebHostExtensions.cs +++ b/samples/GenericWebHost/WebHostExtensions.cs @@ -22,7 +22,7 @@ namespace GenericWebHost { options.ConfigureApp = configureApp; }); - services.AddSingleton(); + services.AddHostedService(); var listener = new DiagnosticListener("Microsoft.AspNetCore"); services.AddSingleton(listener); diff --git a/src/Microsoft.Extensions.Hosting.Abstractions/ServiceCollectionHostedServiceExtensions.cs b/src/Microsoft.Extensions.Hosting.Abstractions/ServiceCollectionHostedServiceExtensions.cs new file mode 100644 index 0000000000..98bdbd333a --- /dev/null +++ b/src/Microsoft.Extensions.Hosting.Abstractions/ServiceCollectionHostedServiceExtensions.cs @@ -0,0 +1,20 @@ +// 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.Extensions.Hosting; + +namespace Microsoft.Extensions.DependencyInjection +{ + public static class ServiceCollectionHostedServiceExtensions + { + /// + /// Add an registration for the given type. + /// + /// An to register. + /// The to register with. + /// The original . + public static IServiceCollection AddHostedService(this IServiceCollection services) + where THostedService : class, IHostedService + => services.AddTransient(); + } +} diff --git a/test/Microsoft.AspNetCore.Hosting.Tests/WebHostTests.cs b/test/Microsoft.AspNetCore.Hosting.Tests/WebHostTests.cs index 373836e5ea..0bc568277c 100644 --- a/test/Microsoft.AspNetCore.Hosting.Tests/WebHostTests.cs +++ b/test/Microsoft.AspNetCore.Hosting.Tests/WebHostTests.cs @@ -548,7 +548,7 @@ namespace Microsoft.AspNetCore.Hosting .UseFakeServer() .ConfigureServices(services => { - services.AddSingleton(); + services.AddHostedService(); }) .Build()) { diff --git a/test/Microsoft.Extensions.Hosting.Tests/HostTests.cs b/test/Microsoft.Extensions.Hosting.Tests/HostTests.cs index 5f6b5db76e..31a38b81c5 100644 --- a/test/Microsoft.Extensions.Hosting.Tests/HostTests.cs +++ b/test/Microsoft.Extensions.Hosting.Tests/HostTests.cs @@ -110,7 +110,7 @@ namespace Microsoft.Extensions.Hosting using (var host = CreateBuilder() .ConfigureServices((hostContext, services) => { - services.AddSingleton(); + services.AddHostedService(); }) .Build()) { @@ -133,7 +133,7 @@ namespace Microsoft.Extensions.Hosting using (var host = CreateBuilder() .ConfigureServices((hostContext, services) => { - services.AddSingleton(); + services.AddHostedService(); }) .Build()) {