From dc29f03548b554ca7fef6abcd8c9ca518d55a042 Mon Sep 17 00:00:00 2001 From: Pranav K Date: Wed, 27 May 2020 12:01:34 -0700 Subject: [PATCH] Replace usages of Assembly.CodeBase with Assembly.Location (#22258) Fixes https://github.com/dotnet/aspnetcore/issues/14827 --- .../StaticWebAssets/StaticWebAssetsLoader.cs | 32 +++---- .../StaticWebAssetsFileProviderTests.cs | 6 +- .../StaticWebAssetsLoaderTests.cs | 2 +- .../Infrastructure/ServerFactory.cs | 2 +- .../RelatedAssemblyAttribute.cs | 15 +--- .../RelatedAssemblyPartTest.cs | 86 ------------------- .../test/IIS.Tests/Utilities/TestServer.cs | 8 +- 7 files changed, 23 insertions(+), 128 deletions(-) diff --git a/src/Hosting/Hosting/src/StaticWebAssets/StaticWebAssetsLoader.cs b/src/Hosting/Hosting/src/StaticWebAssets/StaticWebAssetsLoader.cs index 54f8de9b13..99409c5689 100644 --- a/src/Hosting/Hosting/src/StaticWebAssets/StaticWebAssetsLoader.cs +++ b/src/Hosting/Hosting/src/StaticWebAssets/StaticWebAssetsLoader.cs @@ -1,12 +1,11 @@ // 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; +#nullable enable using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; -using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.FileProviders; @@ -26,12 +25,10 @@ namespace Microsoft.AspNetCore.Hosting.StaticWebAssets /// The host . public static void UseStaticWebAssets(IWebHostEnvironment environment, IConfiguration configuration) { - using (var manifest = ResolveManifest(environment, configuration)) + using var manifest = ResolveManifest(environment, configuration); + if (manifest != null) { - if (manifest != null) - { - UseStaticWebAssetsCore(environment, manifest); - } + UseStaticWebAssetsCore(environment, manifest); } } @@ -56,14 +53,14 @@ namespace Microsoft.AspNetCore.Hosting.StaticWebAssets } } - internal static Stream ResolveManifest(IWebHostEnvironment environment, IConfiguration configuration) + internal static Stream? ResolveManifest(IWebHostEnvironment environment, IConfiguration configuration) { try { var manifestPath = configuration.GetValue(WebHostDefaults.StaticWebAssetsKey); var filePath = manifestPath ?? ResolveRelativeToAssembly(environment); - - if (File.Exists(filePath)) + + if (filePath != null && File.Exists(filePath)) { return File.OpenRead(filePath); } @@ -81,21 +78,16 @@ namespace Microsoft.AspNetCore.Hosting.StaticWebAssets } } - private static string ResolveRelativeToAssembly(IWebHostEnvironment environment) + private static string? ResolveRelativeToAssembly(IWebHostEnvironment environment) { var assembly = Assembly.Load(environment.ApplicationName); - return Path.Combine(Path.GetDirectoryName(GetAssemblyLocation(assembly)), $"{environment.ApplicationName}.StaticWebAssets.xml"); - } - - internal static string GetAssemblyLocation(Assembly assembly) - { - if (Uri.TryCreate(assembly.CodeBase, UriKind.Absolute, out var result) && - result.IsFile && string.IsNullOrWhiteSpace(result.Fragment)) + if (string.IsNullOrEmpty(assembly.Location)) { - return result.LocalPath; + return null; } - return assembly.Location; + return Path.Combine(Path.GetDirectoryName(assembly.Location)!, $"{environment.ApplicationName}.StaticWebAssets.xml"); } } } +#nullable restore diff --git a/src/Hosting/Hosting/test/StaticWebAssets/StaticWebAssetsFileProviderTests.cs b/src/Hosting/Hosting/test/StaticWebAssets/StaticWebAssetsFileProviderTests.cs index a51a85fc0a..dba63c43f3 100644 --- a/src/Hosting/Hosting/test/StaticWebAssets/StaticWebAssetsFileProviderTests.cs +++ b/src/Hosting/Hosting/test/StaticWebAssets/StaticWebAssetsFileProviderTests.cs @@ -153,7 +153,7 @@ namespace Microsoft.AspNetCore.Hosting.StaticWebAssets var expectedResult = RuntimeInformation.IsOSPlatform(OSPlatform.Windows); var provider = new StaticWebAssetsFileProvider( "_cont", - Path.GetDirectoryName(new Uri(typeof(StaticWebAssetsFileProviderTests).Assembly.CodeBase).LocalPath)); + Path.GetDirectoryName(typeof(StaticWebAssetsFileProviderTests).Assembly.Location)); // Act var file = provider.GetFileInfo("/_content/Microsoft.AspNetCore.TestHost.StaticWebAssets.xml"); @@ -169,7 +169,7 @@ namespace Microsoft.AspNetCore.Hosting.StaticWebAssets var expectedResult = RuntimeInformation.IsOSPlatform(OSPlatform.Windows); var provider = new StaticWebAssetsFileProvider( "_content", - Path.GetDirectoryName(new Uri(typeof(StaticWebAssetsFileProviderTests).Assembly.CodeBase).LocalPath)); + Path.GetDirectoryName(typeof(StaticWebAssetsFileProviderTests).Assembly.Location)); // Act var file = provider.GetFileInfo("/_CONTENT/Microsoft.AspNetCore.Hosting.StaticWebAssets.xml"); @@ -185,7 +185,7 @@ namespace Microsoft.AspNetCore.Hosting.StaticWebAssets var expectedResult = RuntimeInformation.IsOSPlatform(OSPlatform.Windows); var provider = new StaticWebAssetsFileProvider( "_content", - Path.GetDirectoryName(new Uri(typeof(StaticWebAssetsFileProviderTests).Assembly.CodeBase).LocalPath)); + Path.GetDirectoryName(typeof(StaticWebAssetsFileProviderTests).Assembly.Location)); // Act var directory = provider.GetDirectoryContents("/_CONTENT"); diff --git a/src/Hosting/Hosting/test/StaticWebAssets/StaticWebAssetsLoaderTests.cs b/src/Hosting/Hosting/test/StaticWebAssets/StaticWebAssetsLoaderTests.cs index 22ccacef50..a7ce4f6812 100644 --- a/src/Hosting/Hosting/test/StaticWebAssets/StaticWebAssetsLoaderTests.cs +++ b/src/Hosting/Hosting/test/StaticWebAssets/StaticWebAssetsLoaderTests.cs @@ -89,7 +89,7 @@ namespace Microsoft.AspNetCore.Hosting.StaticWebAssets "; - var path = Path.ChangeExtension(new Uri(typeof(StaticWebAssetsLoader).Assembly.CodeBase).LocalPath, ".StaticWebAssets.xml"); + var path = Path.ChangeExtension(typeof(StaticWebAssetsLoader).Assembly.Location, ".StaticWebAssets.xml"); var environment = new HostingEnvironment() { ApplicationName = "NonExistingDll" diff --git a/src/Identity/test/Identity.FunctionalTests/Infrastructure/ServerFactory.cs b/src/Identity/test/Identity.FunctionalTests/Infrastructure/ServerFactory.cs index 8258bb2e74..8ce8a29577 100644 --- a/src/Identity/test/Identity.FunctionalTests/Infrastructure/ServerFactory.cs +++ b/src/Identity/test/Identity.FunctionalTests/Infrastructure/ServerFactory.cs @@ -66,7 +66,7 @@ namespace Microsoft.AspNetCore.Identity.FunctionalTests private void UpdateStaticAssets(IWebHostBuilder builder) { - var manifestPath = Path.GetDirectoryName(new Uri(typeof(ServerFactory<,>).Assembly.CodeBase).LocalPath); + var manifestPath = Path.GetDirectoryName(typeof(ServerFactory<,>).Assembly.Location); builder.ConfigureAppConfiguration((ctx, cb) => { if (ctx.HostingEnvironment.WebRootFileProvider is CompositeFileProvider composite) diff --git a/src/Mvc/Mvc.Core/src/ApplicationParts/RelatedAssemblyAttribute.cs b/src/Mvc/Mvc.Core/src/ApplicationParts/RelatedAssemblyAttribute.cs index 294d7b9ac8..0c89c0e49d 100644 --- a/src/Mvc/Mvc.Core/src/ApplicationParts/RelatedAssemblyAttribute.cs +++ b/src/Mvc/Mvc.Core/src/ApplicationParts/RelatedAssemblyAttribute.cs @@ -66,7 +66,7 @@ namespace Microsoft.AspNetCore.Mvc.ApplicationParts // MVC will specifically look for related parts in the same physical directory as the assembly. // No-op if the assembly does not have a location. - if (assembly.IsDynamic || string.IsNullOrEmpty(assembly.CodeBase)) + if (assembly.IsDynamic || string.IsNullOrEmpty(assembly.Location)) { return Array.Empty(); } @@ -78,7 +78,7 @@ namespace Microsoft.AspNetCore.Mvc.ApplicationParts } var assemblyName = assembly.GetName().Name; - var assemblyLocation = GetAssemblyLocation(assembly); + var assemblyLocation = assembly.Location; var assemblyDirectory = Path.GetDirectoryName(assemblyLocation); var relatedAssemblies = new List(); @@ -112,16 +112,5 @@ namespace Microsoft.AspNetCore.Mvc.ApplicationParts return relatedAssemblies; } - - internal static string GetAssemblyLocation(Assembly assembly) - { - if (Uri.TryCreate(assembly.CodeBase, UriKind.Absolute, out var result) && - result.IsFile && string.IsNullOrWhiteSpace(result.Fragment)) - { - return result.LocalPath; - } - - return assembly.Location; - } } } diff --git a/src/Mvc/Mvc.Core/test/ApplicationParts/RelatedAssemblyPartTest.cs b/src/Mvc/Mvc.Core/test/ApplicationParts/RelatedAssemblyPartTest.cs index 6819818bf1..33d5e9d0e5 100644 --- a/src/Mvc/Mvc.Core/test/ApplicationParts/RelatedAssemblyPartTest.cs +++ b/src/Mvc/Mvc.Core/test/ApplicationParts/RelatedAssemblyPartTest.cs @@ -75,88 +75,6 @@ namespace Microsoft.AspNetCore.Mvc.ApplicationParts Assert.Equal(new[] { relatedAssembly }, result); } - [Fact] - public void GetAssemblyLocation_UsesCodeBase() - { - // Arrange - var destination = Path.Combine(AssemblyDirectory, "RelatedAssembly.dll"); - var codeBase = "file://x:/file/Assembly.dll"; - var expected = new Uri(codeBase).LocalPath; - var assembly = new TestAssembly - { - CodeBaseSettable = codeBase, - }; - - // Act - var actual = RelatedAssemblyAttribute.GetAssemblyLocation(assembly); - Assert.Equal(expected, actual); - } - - [Fact] - public void GetAssemblyLocation_UsesLocation_IfCodeBaseIsNotLocal() - { - // Arrange - var destination = Path.Combine(AssemblyDirectory, "RelatedAssembly.dll"); - var expected = Path.Combine(AssemblyDirectory, "Some-Dir", "Assembly.dll"); - var assembly = new TestAssembly - { - CodeBaseSettable = "https://www.microsoft.com/test.dll", - LocationSettable = expected, - }; - - // Act - var actual = RelatedAssemblyAttribute.GetAssemblyLocation(assembly); - Assert.Equal(expected, actual); - } - - [Fact] - public void GetAssemblyLocation_CodeBase_HasPoundCharacterUnixPath() - { - var destination = Path.Combine(AssemblyDirectory, "RelatedAssembly.dll"); - var expected = @"/etc/#NIN/dotnetcore/tryx/try1.dll"; - var assembly = new TestAssembly - { - CodeBaseSettable = "file:///etc/#NIN/dotnetcore/tryx/try1.dll", - LocationSettable = expected, - }; - - // Act - var actual = RelatedAssemblyAttribute.GetAssemblyLocation(assembly); - Assert.Equal(expected, actual); - } - - [Fact] - public void GetAssemblyLocation_CodeBase_HasPoundCharacterUNCPath() - { - var destination = Path.Combine(AssemblyDirectory, "RelatedAssembly.dll"); - var expected = @"\\server\#NIN\dotnetcore\tryx\try1.dll"; - var assembly = new TestAssembly - { - CodeBaseSettable = "file://server/#NIN/dotnetcore/tryx/try1.dll", - LocationSettable = expected, - }; - - // Act - var actual = RelatedAssemblyAttribute.GetAssemblyLocation(assembly); - Assert.Equal(expected, actual); - } - - [Fact] - public void GetAssemblyLocation_CodeBase_HasPoundCharacterDOSPath() - { - var destination = Path.Combine(AssemblyDirectory, "RelatedAssembly.dll"); - var expected = @"C:\#NIN\dotnetcore\tryx\try1.dll"; - var assembly = new TestAssembly - { - CodeBaseSettable = "file:///C:/#NIN/dotnetcore/tryx/try1.dll", - LocationSettable = expected, - }; - - // Act - var actual = RelatedAssemblyAttribute.GetAssemblyLocation(assembly); - Assert.Equal(expected, actual); - } - private class TestAssembly : Assembly { public override AssemblyName GetName() @@ -166,10 +84,6 @@ namespace Microsoft.AspNetCore.Mvc.ApplicationParts public string AttributeAssembly { get; set; } - public string CodeBaseSettable { get; set; } = Path.Combine(AssemblyDirectory, "MyAssembly.dll"); - - public override string CodeBase => CodeBaseSettable; - public string LocationSettable { get; set; } = Path.Combine(AssemblyDirectory, "MyAssembly.dll"); public override string Location => LocationSettable; diff --git a/src/Servers/IIS/IIS/test/IIS.Tests/Utilities/TestServer.cs b/src/Servers/IIS/IIS/test/IIS.Tests/Utilities/TestServer.cs index 37f871f6a7..64415084d1 100644 --- a/src/Servers/IIS/IIS/test/IIS.Tests/Utilities/TestServer.cs +++ b/src/Servers/IIS/IIS/test/IIS.Tests/Utilities/TestServer.cs @@ -30,7 +30,7 @@ namespace Microsoft.AspNetCore.Server.IIS.FunctionalTests private const string HWebCoreDll = "hwebcore.dll"; internal static string HostableWebCoreLocation => Environment.ExpandEnvironmentVariables($@"%windir%\system32\inetsrv\{HWebCoreDll}"); - internal static string BasePath => Path.Combine(Path.GetDirectoryName(new Uri(typeof(TestServer).Assembly.CodeBase).AbsolutePath), + internal static string BasePath => Path.Combine(Path.GetDirectoryName(typeof(TestServer).Assembly.Location), "ANCM", Environment.Is64BitProcess ? "x64" : "x86"); @@ -45,7 +45,7 @@ namespace Microsoft.AspNetCore.Server.IIS.FunctionalTests private readonly Action _appBuilder; private readonly ILoggerFactory _loggerFactory; private readonly hostfxr_main_fn _hostfxrMainFn; - + private Uri BaseUri => new Uri("http://localhost:" + _currentPort); public HttpClient HttpClient { get; private set; } public TestConnection CreateConnection() => new TestConnection(_currentPort); @@ -88,7 +88,7 @@ namespace Microsoft.AspNetCore.Server.IIS.FunctionalTests { LoadLibrary(HostableWebCoreLocation); _appHostConfigPath = Path.GetTempFileName(); - + set_main_handler(_hostfxrMainFn); Retry(() => @@ -159,7 +159,7 @@ namespace Microsoft.AspNetCore.Server.IIS.FunctionalTests // WebCoreShutdown occasionally AVs // This causes the dotnet test process to crash - // To avoid this, we have to wait to shutdown + // To avoid this, we have to wait to shutdown // and pass in true to immediately shutdown the hostable web core // Both of these seem to be required. Thread.Sleep(100);