diff --git a/MetaPackages.sln b/MetaPackages.sln index 2528f60..1404087 100644 --- a/MetaPackages.sln +++ b/MetaPackages.sln @@ -1,6 +1,6 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 -VisualStudioVersion = 15.0.26424.2 +VisualStudioVersion = 15.0.26412.1 MinimumVisualStudioVersion = 10.0.40219.1 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{ED834E68-51C3-4ADE-ACC8-6BA6D4207C09}" EndProject @@ -44,6 +44,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.DotNet.Archive", EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TrimDeps", "tools\TrimDeps\TrimDeps.csproj", "{67E4C92F-6D12-4C52-BB79-B8D11BFC6B82}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DependencyInjectionApp", "test\TestSites\DependencyInjectionApp\DependencyInjectionApp.csproj", "{65FE2E38-4529-4C93-A7B0-CF12DD7A70C3}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -94,6 +96,10 @@ Global {67E4C92F-6D12-4C52-BB79-B8D11BFC6B82}.Debug|Any CPU.Build.0 = Debug|Any CPU {67E4C92F-6D12-4C52-BB79-B8D11BFC6B82}.Release|Any CPU.ActiveCfg = Release|Any CPU {67E4C92F-6D12-4C52-BB79-B8D11BFC6B82}.Release|Any CPU.Build.0 = Release|Any CPU + {65FE2E38-4529-4C93-A7B0-CF12DD7A70C3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {65FE2E38-4529-4C93-A7B0-CF12DD7A70C3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {65FE2E38-4529-4C93-A7B0-CF12DD7A70C3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {65FE2E38-4529-4C93-A7B0-CF12DD7A70C3}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -112,5 +118,7 @@ Global {AE4216BF-D471-471B-82F3-6B6D004F7D17} = {ED834E68-51C3-4ADE-ACC8-6BA6D4207C09} {302400A0-98BB-4C04-88D4-C32DC2D4B945} = {ED834E68-51C3-4ADE-ACC8-6BA6D4207C09} {67E4C92F-6D12-4C52-BB79-B8D11BFC6B82} = {ED834E68-51C3-4ADE-ACC8-6BA6D4207C09} + {302400A0-98BB-4C04-88D4-C32DC2D4B945} = {ED834E68-51C3-4ADE-ACC8-6BA6D4207C09} + {65FE2E38-4529-4C93-A7B0-CF12DD7A70C3} = {EC22261D-0DE1-47DE-8F7C-072675D6F5B4} EndGlobalSection EndGlobal diff --git a/test/Microsoft.AspNetCore.FunctionalTests/WebHostFunctionalTests.cs b/test/Microsoft.AspNetCore.FunctionalTests/WebHostFunctionalTests.cs index 39101ac..e7074a1 100644 --- a/test/Microsoft.AspNetCore.FunctionalTests/WebHostFunctionalTests.cs +++ b/test/Microsoft.AspNetCore.FunctionalTests/WebHostFunctionalTests.cs @@ -73,6 +73,30 @@ await ExecuteTestApp(applicationName, async (deploymentResult, logger) => }, setTestEnvVars: true); } + [Theory] + [InlineData("Development", "InvalidOperationException: Cannot consume scoped service")] + [InlineData("Production", "Success")] + public async Task CreateDefaultBuilder_InitializesDependencyInjectionSettingsBasedOnEnv(string environment, string expected) + { + var applicationName = "DependencyInjectionApp"; + await ExecuteTestApp(applicationName, async (deploymentResult, logger) => + { + var response = await RetryHelper.RetryRequest(() => deploymentResult.HttpClient.GetAsync(string.Empty), logger, deploymentResult.HostShutdownToken); + var responseText = await response.Content.ReadAsStringAsync(); + try + { + // Assert UseDeveloperExceptionPage is called in WebHostStartupFilter. + Assert.Contains(expected, responseText); + } + catch (XunitException) + { + logger.LogWarning(response.ToString()); + logger.LogWarning(responseText); + throw; + } + }, setTestEnvVars: true, environment: environment); + } + [Theory] [InlineData("127.0.0.1", "127.0.0.1")] [InlineData("::1", "[::1]")] @@ -247,7 +271,10 @@ await ExecuteTestApp(applicationName, async (deploymentResult, logger) => }); } - private async Task ExecuteTestApp(string applicationName, Func assertAction, bool setTestEnvVars = false) + private async Task ExecuteTestApp(string applicationName, + Func assertAction, + bool setTestEnvVars = false, + string environment = "Development") { using (StartLog(out var loggerFactory, applicationName)) { @@ -256,7 +283,7 @@ private async Task ExecuteTestApp(string applicationName, Func("aspnetcore_environment", "Development")); + deploymentParameters.EnvironmentVariables.Add(new KeyValuePair("aspnetcore_environment", environment)); deploymentParameters.EnvironmentVariables.Add(new KeyValuePair("envKey", "envValue")); } diff --git a/test/TestSites/DependencyInjectionApp/DependencyInjectionApp.csproj b/test/TestSites/DependencyInjectionApp/DependencyInjectionApp.csproj new file mode 100644 index 0000000..04691e8 --- /dev/null +++ b/test/TestSites/DependencyInjectionApp/DependencyInjectionApp.csproj @@ -0,0 +1,12 @@ + + + + Exe + netcoreapp2.0 + + + + + + + diff --git a/test/TestSites/DependencyInjectionApp/Program.cs b/test/TestSites/DependencyInjectionApp/Program.cs new file mode 100644 index 0000000..555dcc7 --- /dev/null +++ b/test/TestSites/DependencyInjectionApp/Program.cs @@ -0,0 +1,67 @@ +// 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 Microsoft.AspNetCore; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; +using Microsoft.Extensions.Logging.Console; +using Microsoft.Extensions.Logging.Debug; + +namespace CreateDefaultBuilderApp +{ + public class Program + { + static void Main(string[] args) + { + WebHost.CreateDefaultBuilder() + .ConfigureServices((context, services) => + { + services.AddSingleton(typeof(IService<>), typeof(Service<>)); + services.AddScoped(); + }) + .Configure(app => + { + app.Run(context => + { + try + { + context.RequestServices.GetService>(); + return context.Response.WriteAsync("Success"); + } + catch (Exception ex) + { + return context.Response.WriteAsync(ex.ToString()); + } + }); + }) + .Build().Run(); + } + + interface IService + { + } + + interface IAnotherService + { + } + + class Service: IService + { + public Service(T t) + { + } + } + + class AnotherService: IAnotherService + { + } + } +}