From ce650eee7f9a5e87e27baaf66a8a2d373530ded3 Mon Sep 17 00:00:00 2001 From: Andrew Stanton-Nurse Date: Thu, 20 Apr 2017 09:30:40 -0700 Subject: [PATCH] react to removal of PlatformAbstractions (#1023) --- .../Microsoft.AspNetCore.Hosting.csproj | 1 - .../WebHostBuilder.cs | 8 ++- .../Deployers/ApplicationDeployer.cs | 39 +++++++++++---- ...spNetCore.Server.IntegrationTesting.csproj | 2 - .../xunit/TestProjectHelpers.cs | 4 +- .../Microsoft.AspNetCore.Hosting.Tests.csproj | 1 - .../WebHostBuilderTests.cs | 50 ++++++++++++++----- 7 files changed, 70 insertions(+), 35 deletions(-) diff --git a/src/Microsoft.AspNetCore.Hosting/Microsoft.AspNetCore.Hosting.csproj b/src/Microsoft.AspNetCore.Hosting/Microsoft.AspNetCore.Hosting.csproj index 1f5c69fcb4..a5270b55b8 100644 --- a/src/Microsoft.AspNetCore.Hosting/Microsoft.AspNetCore.Hosting.csproj +++ b/src/Microsoft.AspNetCore.Hosting/Microsoft.AspNetCore.Hosting.csproj @@ -26,7 +26,6 @@ - diff --git a/src/Microsoft.AspNetCore.Hosting/WebHostBuilder.cs b/src/Microsoft.AspNetCore.Hosting/WebHostBuilder.cs index dc1bee834d..336cec3f40 100644 --- a/src/Microsoft.AspNetCore.Hosting/WebHostBuilder.cs +++ b/src/Microsoft.AspNetCore.Hosting/WebHostBuilder.cs @@ -1,4 +1,4 @@ -// 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; @@ -15,7 +15,6 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Logging; using Microsoft.Extensions.ObjectPool; -using Microsoft.Extensions.PlatformAbstractions; namespace Microsoft.AspNetCore.Hosting { @@ -274,9 +273,8 @@ namespace Microsoft.AspNetCore.Hosting _options = new WebHostOptions(_config); - var appEnvironment = PlatformServices.Default.Application; - var contentRootPath = ResolveContentRootPath(_options.ContentRootPath, appEnvironment.ApplicationBasePath); - var applicationName = _options.ApplicationName ?? appEnvironment.ApplicationName; + var contentRootPath = ResolveContentRootPath(_options.ContentRootPath, AppContext.BaseDirectory); + var applicationName = _options.ApplicationName; // Initialize the hosting environment _hostingEnvironment.Initialize(applicationName, contentRootPath, _options); diff --git a/src/Microsoft.AspNetCore.Server.IntegrationTesting/Deployers/ApplicationDeployer.cs b/src/Microsoft.AspNetCore.Server.IntegrationTesting/Deployers/ApplicationDeployer.cs index 4cef40cd69..aa31a3f631 100644 --- a/src/Microsoft.AspNetCore.Server.IntegrationTesting/Deployers/ApplicationDeployer.cs +++ b/src/Microsoft.AspNetCore.Server.IntegrationTesting/Deployers/ApplicationDeployer.cs @@ -5,11 +5,11 @@ using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; +using System.Runtime.InteropServices; using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Internal; using Microsoft.Extensions.Logging; -using PlatformAbstractions = Microsoft.DotNet.PlatformAbstractions; namespace Microsoft.AspNetCore.Server.IntegrationTesting { @@ -214,18 +214,35 @@ namespace Microsoft.AspNetCore.Server.IntegrationTesting private string GetRuntimeIdentifier() { - var architecture = PlatformAbstractions.RuntimeEnvironment.RuntimeArchitecture; - switch (PlatformAbstractions.RuntimeEnvironment.OperatingSystemPlatform) + var architecture = GetArchitecture(); + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { - case PlatformAbstractions.Platform.Windows: - return "win7-" + architecture; - case PlatformAbstractions.Platform.Linux: - return "linux-" + architecture; - case PlatformAbstractions.Platform.Darwin: - return "osx.10.12-" + architecture; + return "win7-" + architecture; + } + else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + { + return "linux-" + architecture; + } + else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) + { + return "osx.10.12-" + architecture; + } + else + { + throw new InvalidOperationException("Unrecognized operation system platform"); + } + } + + private string GetArchitecture() + { + switch (RuntimeInformation.OSArchitecture) + { + case Architecture.X86: + return "x86"; + case Architecture.X64: + return "x64"; default: - throw new InvalidOperationException( - "Unrecognized operation system platform: " + PlatformAbstractions.RuntimeEnvironment.OperatingSystemPlatform); + throw new NotSupportedException($"Unsupported architecture: {RuntimeInformation.OSArchitecture}"); } } } diff --git a/src/Microsoft.AspNetCore.Server.IntegrationTesting/Microsoft.AspNetCore.Server.IntegrationTesting.csproj b/src/Microsoft.AspNetCore.Server.IntegrationTesting/Microsoft.AspNetCore.Server.IntegrationTesting.csproj index 109c834ec3..7901a30fed 100644 --- a/src/Microsoft.AspNetCore.Server.IntegrationTesting/Microsoft.AspNetCore.Server.IntegrationTesting.csproj +++ b/src/Microsoft.AspNetCore.Server.IntegrationTesting/Microsoft.AspNetCore.Server.IntegrationTesting.csproj @@ -23,8 +23,6 @@ - - diff --git a/src/Microsoft.AspNetCore.Server.IntegrationTesting/xunit/TestProjectHelpers.cs b/src/Microsoft.AspNetCore.Server.IntegrationTesting/xunit/TestProjectHelpers.cs index 697d628c88..837e34497c 100644 --- a/src/Microsoft.AspNetCore.Server.IntegrationTesting/xunit/TestProjectHelpers.cs +++ b/src/Microsoft.AspNetCore.Server.IntegrationTesting/xunit/TestProjectHelpers.cs @@ -3,7 +3,6 @@ using System; using System.IO; -using Microsoft.Extensions.PlatformAbstractions; namespace Microsoft.AspNetCore.Server.IntegrationTesting.xunit { @@ -13,8 +12,7 @@ namespace Microsoft.AspNetCore.Server.IntegrationTesting.xunit public static string GetSolutionRoot() { - var applicationName = PlatformServices.Default.Application.ApplicationName; - var applicationBasePath = PlatformServices.Default.Application.ApplicationBasePath; + var applicationBasePath = AppContext.BaseDirectory; var directoryInfo = new DirectoryInfo(applicationBasePath); do diff --git a/test/Microsoft.AspNetCore.Hosting.Tests/Microsoft.AspNetCore.Hosting.Tests.csproj b/test/Microsoft.AspNetCore.Hosting.Tests/Microsoft.AspNetCore.Hosting.Tests.csproj index 436e449845..37912abcaa 100644 --- a/test/Microsoft.AspNetCore.Hosting.Tests/Microsoft.AspNetCore.Hosting.Tests.csproj +++ b/test/Microsoft.AspNetCore.Hosting.Tests/Microsoft.AspNetCore.Hosting.Tests.csproj @@ -20,7 +20,6 @@ - diff --git a/test/Microsoft.AspNetCore.Hosting.Tests/WebHostBuilderTests.cs b/test/Microsoft.AspNetCore.Hosting.Tests/WebHostBuilderTests.cs index 5cc6c1e769..c2eb9c4364 100644 --- a/test/Microsoft.AspNetCore.Hosting.Tests/WebHostBuilderTests.cs +++ b/test/Microsoft.AspNetCore.Hosting.Tests/WebHostBuilderTests.cs @@ -1,4 +1,4 @@ -// 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; @@ -21,7 +21,6 @@ using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Logging.Testing; using Microsoft.Extensions.ObjectPool; -using Microsoft.Extensions.PlatformAbstractions; using Xunit; [assembly: HostingStartup(typeof(WebHostBuilderTests.TestHostingStartup))] @@ -650,52 +649,79 @@ namespace Microsoft.AspNetCore.Hosting .UseStartup("Microsoft.AspNetCore.Hosting.Tests") .Build()) { - var appBase = PlatformServices.Default.Application.ApplicationBasePath; + var appBase = AppContext.BaseDirectory; Assert.Equal(appBase, host.Services.GetService().ContentRootPath); } } [Fact] - public void DefaultApplicationNameToStartupAssemblyName() + public void DefaultApplicationNameWithNoStartupThrows() + { + var builder = new ConfigurationBuilder(); + var host = new WebHostBuilder() + .UseServer(new TestServer()); + + var ex = Assert.Throws(() => host.Build()); + + // ArgumentException adds "Parameter name" to the message and this is the cleanest way to make sure we get the right + // expected string + Assert.Equal(new ArgumentException("A valid non-empty application name must be provided.", "applicationName").Message , ex.Message); + } + + [Fact] + public void DefaultApplicationNameWithUseStartupOfString() { var builder = new ConfigurationBuilder(); using (var host = new WebHostBuilder() .UseServer(new TestServer()) - .UseStartup("Microsoft.AspNetCore.Hosting.Tests") + .UseStartup(typeof(Startup).Assembly.GetName().Name) .Build()) { var hostingEnv = host.Services.GetService(); - Assert.Equal("Microsoft.AspNetCore.Hosting.Tests", hostingEnv.ApplicationName); + Assert.Equal(typeof(Startup).Assembly.GetName().Name, hostingEnv.ApplicationName); } } [Fact] - public void DefaultApplicationNameToStartupType() + public void DefaultApplicationNameWithUseStartupOfT() { var builder = new ConfigurationBuilder(); using (var host = new WebHostBuilder() .UseServer(new TestServer()) .UseStartup() - .UseStartup("Microsoft.AspNetCore.Hosting.Tests.NonExistent") .Build()) { var hostingEnv = host.Services.GetService(); - Assert.Equal("Microsoft.AspNetCore.Hosting.Tests.NonExistent", hostingEnv.ApplicationName); + Assert.Equal(typeof(StartupNoServices).Assembly.GetName().Name, hostingEnv.ApplicationName); } } [Fact] - public void DefaultApplicationNameAndBasePathToStartupMethods() + public void DefaultApplicationNameWithUseStartupOfType() + { + var builder = new ConfigurationBuilder(); + var host = new WebHostBuilder() + .UseServer(new TestServer()) + .UseStartup(typeof(StartupNoServices)) + .Build(); + + var hostingEnv = host.Services.GetService(); + Assert.Equal(typeof(StartupNoServices).Assembly.GetName().Name, hostingEnv.ApplicationName); + } + + [Fact] + public void DefaultApplicationNameWithConfigure() { var builder = new ConfigurationBuilder(); using (var host = new WebHostBuilder() .UseServer(new TestServer()) .Configure(app => { }) - .UseStartup("Microsoft.AspNetCore.Hosting.Tests.NonExistent") .Build()) { var hostingEnv = host.Services.GetService(); - Assert.Equal("Microsoft.AspNetCore.Hosting.Tests.NonExistent", hostingEnv.ApplicationName); + + // Should be the assembly containing this test, because that's where the delegate comes from + Assert.Equal(typeof(WebHostBuilderTests).Assembly.GetName().Name, hostingEnv.ApplicationName); } }