Replace usages of Assembly.CodeBase with Assembly.Location (#22258)
Fixes https://github.com/dotnet/aspnetcore/issues/14827
This commit is contained in:
parent
e212773b74
commit
dc29f03548
|
|
@ -1,12 +1,11 @@
|
||||||
// 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.
|
// 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.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.FileProviders;
|
using Microsoft.Extensions.FileProviders;
|
||||||
|
|
||||||
|
|
@ -26,12 +25,10 @@ namespace Microsoft.AspNetCore.Hosting.StaticWebAssets
|
||||||
/// <param name="configuration">The host <see cref="IConfiguration"/>.</param>
|
/// <param name="configuration">The host <see cref="IConfiguration"/>.</param>
|
||||||
public static void UseStaticWebAssets(IWebHostEnvironment environment, IConfiguration configuration)
|
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
|
try
|
||||||
{
|
{
|
||||||
var manifestPath = configuration.GetValue<string>(WebHostDefaults.StaticWebAssetsKey);
|
var manifestPath = configuration.GetValue<string>(WebHostDefaults.StaticWebAssetsKey);
|
||||||
var filePath = manifestPath ?? ResolveRelativeToAssembly(environment);
|
var filePath = manifestPath ?? ResolveRelativeToAssembly(environment);
|
||||||
|
|
||||||
if (File.Exists(filePath))
|
if (filePath != null && File.Exists(filePath))
|
||||||
{
|
{
|
||||||
return File.OpenRead(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);
|
var assembly = Assembly.Load(environment.ApplicationName);
|
||||||
return Path.Combine(Path.GetDirectoryName(GetAssemblyLocation(assembly)), $"{environment.ApplicationName}.StaticWebAssets.xml");
|
if (string.IsNullOrEmpty(assembly.Location))
|
||||||
}
|
|
||||||
|
|
||||||
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 null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return assembly.Location;
|
return Path.Combine(Path.GetDirectoryName(assembly.Location)!, $"{environment.ApplicationName}.StaticWebAssets.xml");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#nullable restore
|
||||||
|
|
|
||||||
|
|
@ -153,7 +153,7 @@ namespace Microsoft.AspNetCore.Hosting.StaticWebAssets
|
||||||
var expectedResult = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
|
var expectedResult = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
|
||||||
var provider = new StaticWebAssetsFileProvider(
|
var provider = new StaticWebAssetsFileProvider(
|
||||||
"_cont",
|
"_cont",
|
||||||
Path.GetDirectoryName(new Uri(typeof(StaticWebAssetsFileProviderTests).Assembly.CodeBase).LocalPath));
|
Path.GetDirectoryName(typeof(StaticWebAssetsFileProviderTests).Assembly.Location));
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var file = provider.GetFileInfo("/_content/Microsoft.AspNetCore.TestHost.StaticWebAssets.xml");
|
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 expectedResult = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
|
||||||
var provider = new StaticWebAssetsFileProvider(
|
var provider = new StaticWebAssetsFileProvider(
|
||||||
"_content",
|
"_content",
|
||||||
Path.GetDirectoryName(new Uri(typeof(StaticWebAssetsFileProviderTests).Assembly.CodeBase).LocalPath));
|
Path.GetDirectoryName(typeof(StaticWebAssetsFileProviderTests).Assembly.Location));
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var file = provider.GetFileInfo("/_CONTENT/Microsoft.AspNetCore.Hosting.StaticWebAssets.xml");
|
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 expectedResult = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
|
||||||
var provider = new StaticWebAssetsFileProvider(
|
var provider = new StaticWebAssetsFileProvider(
|
||||||
"_content",
|
"_content",
|
||||||
Path.GetDirectoryName(new Uri(typeof(StaticWebAssetsFileProviderTests).Assembly.CodeBase).LocalPath));
|
Path.GetDirectoryName(typeof(StaticWebAssetsFileProviderTests).Assembly.Location));
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var directory = provider.GetDirectoryContents("/_CONTENT");
|
var directory = provider.GetDirectoryContents("/_CONTENT");
|
||||||
|
|
|
||||||
|
|
@ -89,7 +89,7 @@ namespace Microsoft.AspNetCore.Hosting.StaticWebAssets
|
||||||
<ContentRoot Path=""/Path"" BasePath=""/BasePath"" />
|
<ContentRoot Path=""/Path"" BasePath=""/BasePath"" />
|
||||||
</StaticWebAssets>
|
</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()
|
var environment = new HostingEnvironment()
|
||||||
{
|
{
|
||||||
ApplicationName = "NonExistingDll"
|
ApplicationName = "NonExistingDll"
|
||||||
|
|
|
||||||
|
|
@ -66,7 +66,7 @@ namespace Microsoft.AspNetCore.Identity.FunctionalTests
|
||||||
|
|
||||||
private void UpdateStaticAssets(IWebHostBuilder builder)
|
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) =>
|
builder.ConfigureAppConfiguration((ctx, cb) =>
|
||||||
{
|
{
|
||||||
if (ctx.HostingEnvironment.WebRootFileProvider is CompositeFileProvider composite)
|
if (ctx.HostingEnvironment.WebRootFileProvider is CompositeFileProvider composite)
|
||||||
|
|
|
||||||
|
|
@ -66,7 +66,7 @@ namespace Microsoft.AspNetCore.Mvc.ApplicationParts
|
||||||
|
|
||||||
// MVC will specifically look for related parts in the same physical directory as the assembly.
|
// 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.
|
// 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<Assembly>();
|
return Array.Empty<Assembly>();
|
||||||
}
|
}
|
||||||
|
|
@ -78,7 +78,7 @@ namespace Microsoft.AspNetCore.Mvc.ApplicationParts
|
||||||
}
|
}
|
||||||
|
|
||||||
var assemblyName = assembly.GetName().Name;
|
var assemblyName = assembly.GetName().Name;
|
||||||
var assemblyLocation = GetAssemblyLocation(assembly);
|
var assemblyLocation = assembly.Location;
|
||||||
var assemblyDirectory = Path.GetDirectoryName(assemblyLocation);
|
var assemblyDirectory = Path.GetDirectoryName(assemblyLocation);
|
||||||
|
|
||||||
var relatedAssemblies = new List<Assembly>();
|
var relatedAssemblies = new List<Assembly>();
|
||||||
|
|
@ -112,16 +112,5 @@ namespace Microsoft.AspNetCore.Mvc.ApplicationParts
|
||||||
|
|
||||||
return relatedAssemblies;
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -75,88 +75,6 @@ namespace Microsoft.AspNetCore.Mvc.ApplicationParts
|
||||||
Assert.Equal(new[] { relatedAssembly }, result);
|
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
|
private class TestAssembly : Assembly
|
||||||
{
|
{
|
||||||
public override AssemblyName GetName()
|
public override AssemblyName GetName()
|
||||||
|
|
@ -166,10 +84,6 @@ namespace Microsoft.AspNetCore.Mvc.ApplicationParts
|
||||||
|
|
||||||
public string AttributeAssembly { get; set; }
|
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 string LocationSettable { get; set; } = Path.Combine(AssemblyDirectory, "MyAssembly.dll");
|
||||||
|
|
||||||
public override string Location => LocationSettable;
|
public override string Location => LocationSettable;
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,7 @@ namespace Microsoft.AspNetCore.Server.IIS.FunctionalTests
|
||||||
private const string HWebCoreDll = "hwebcore.dll";
|
private const string HWebCoreDll = "hwebcore.dll";
|
||||||
|
|
||||||
internal static string HostableWebCoreLocation => Environment.ExpandEnvironmentVariables($@"%windir%\system32\inetsrv\{HWebCoreDll}");
|
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",
|
"ANCM",
|
||||||
Environment.Is64BitProcess ? "x64" : "x86");
|
Environment.Is64BitProcess ? "x64" : "x86");
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue