Skip to content

Commit dc29f03

Browse files
authored
Replace usages of Assembly.CodeBase with Assembly.Location (#22258)
Fixes #14827
1 parent e212773 commit dc29f03

File tree

7 files changed

+23
-128
lines changed

7 files changed

+23
-128
lines changed

src/Hosting/Hosting/src/StaticWebAssets/StaticWebAssetsLoader.cs

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
using System;
4+
#nullable enable
55
using System.Collections.Generic;
66
using System.IO;
77
using System.Linq;
88
using System.Reflection;
9-
using Microsoft.AspNetCore.Hosting;
109
using Microsoft.Extensions.Configuration;
1110
using Microsoft.Extensions.FileProviders;
1211

@@ -26,12 +25,10 @@ public class StaticWebAssetsLoader
2625
/// <param name="configuration">The host <see cref="IConfiguration"/>.</param>
2726
public static void UseStaticWebAssets(IWebHostEnvironment environment, IConfiguration configuration)
2827
{
29-
using (var manifest = ResolveManifest(environment, configuration))
28+
using var manifest = ResolveManifest(environment, configuration);
29+
if (manifest != null)
3030
{
31-
if (manifest != null)
32-
{
33-
UseStaticWebAssetsCore(environment, manifest);
34-
}
31+
UseStaticWebAssetsCore(environment, manifest);
3532
}
3633
}
3734

@@ -56,14 +53,14 @@ internal static void UseStaticWebAssetsCore(IWebHostEnvironment environment, Str
5653
}
5754
}
5855

59-
internal static Stream ResolveManifest(IWebHostEnvironment environment, IConfiguration configuration)
56+
internal static Stream? ResolveManifest(IWebHostEnvironment environment, IConfiguration configuration)
6057
{
6158
try
6259
{
6360
var manifestPath = configuration.GetValue<string>(WebHostDefaults.StaticWebAssetsKey);
6461
var filePath = manifestPath ?? ResolveRelativeToAssembly(environment);
65-
66-
if (File.Exists(filePath))
62+
63+
if (filePath != null && File.Exists(filePath))
6764
{
6865
return File.OpenRead(filePath);
6966
}
@@ -81,21 +78,16 @@ internal static Stream ResolveManifest(IWebHostEnvironment environment, IConfigu
8178
}
8279
}
8380

84-
private static string ResolveRelativeToAssembly(IWebHostEnvironment environment)
81+
private static string? ResolveRelativeToAssembly(IWebHostEnvironment environment)
8582
{
8683
var assembly = Assembly.Load(environment.ApplicationName);
87-
return Path.Combine(Path.GetDirectoryName(GetAssemblyLocation(assembly)), $"{environment.ApplicationName}.StaticWebAssets.xml");
88-
}
89-
90-
internal static string GetAssemblyLocation(Assembly assembly)
91-
{
92-
if (Uri.TryCreate(assembly.CodeBase, UriKind.Absolute, out var result) &&
93-
result.IsFile && string.IsNullOrWhiteSpace(result.Fragment))
84+
if (string.IsNullOrEmpty(assembly.Location))
9485
{
95-
return result.LocalPath;
86+
return null;
9687
}
9788

98-
return assembly.Location;
89+
return Path.Combine(Path.GetDirectoryName(assembly.Location)!, $"{environment.ApplicationName}.StaticWebAssets.xml");
9990
}
10091
}
10192
}
93+
#nullable restore

src/Hosting/Hosting/test/StaticWebAssets/StaticWebAssetsFileProviderTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public void GetFileInfo_DoesNotMatch_IncompletePrefixSegments()
153153
var expectedResult = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
154154
var provider = new StaticWebAssetsFileProvider(
155155
"_cont",
156-
Path.GetDirectoryName(new Uri(typeof(StaticWebAssetsFileProviderTests).Assembly.CodeBase).LocalPath));
156+
Path.GetDirectoryName(typeof(StaticWebAssetsFileProviderTests).Assembly.Location));
157157

158158
// Act
159159
var file = provider.GetFileInfo("/_content/Microsoft.AspNetCore.TestHost.StaticWebAssets.xml");
@@ -169,7 +169,7 @@ public void GetFileInfo_Prefix_RespectsOsCaseSensitivity()
169169
var expectedResult = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
170170
var provider = new StaticWebAssetsFileProvider(
171171
"_content",
172-
Path.GetDirectoryName(new Uri(typeof(StaticWebAssetsFileProviderTests).Assembly.CodeBase).LocalPath));
172+
Path.GetDirectoryName(typeof(StaticWebAssetsFileProviderTests).Assembly.Location));
173173

174174
// Act
175175
var file = provider.GetFileInfo("/_CONTENT/Microsoft.AspNetCore.Hosting.StaticWebAssets.xml");
@@ -185,7 +185,7 @@ public void GetDirectoryContents_Prefix_RespectsOsCaseSensitivity()
185185
var expectedResult = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
186186
var provider = new StaticWebAssetsFileProvider(
187187
"_content",
188-
Path.GetDirectoryName(new Uri(typeof(StaticWebAssetsFileProviderTests).Assembly.CodeBase).LocalPath));
188+
Path.GetDirectoryName(typeof(StaticWebAssetsFileProviderTests).Assembly.Location));
189189

190190
// Act
191191
var directory = provider.GetDirectoryContents("/_CONTENT");

src/Hosting/Hosting/test/StaticWebAssets/StaticWebAssetsLoaderTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public void ResolveManifest_UsesConfigurationKey_WhenProvided()
8989
<ContentRoot Path=""/Path"" BasePath=""/BasePath"" />
9090
</StaticWebAssets>
9191
";
92-
var path = Path.ChangeExtension(new Uri(typeof(StaticWebAssetsLoader).Assembly.CodeBase).LocalPath, ".StaticWebAssets.xml");
92+
var path = Path.ChangeExtension(typeof(StaticWebAssetsLoader).Assembly.Location, ".StaticWebAssets.xml");
9393
var environment = new HostingEnvironment()
9494
{
9595
ApplicationName = "NonExistingDll"

src/Identity/test/Identity.FunctionalTests/Infrastructure/ServerFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ private void UpdateApplicationParts(IWebHostBuilder builder) =>
6666

6767
private void UpdateStaticAssets(IWebHostBuilder builder)
6868
{
69-
var manifestPath = Path.GetDirectoryName(new Uri(typeof(ServerFactory<,>).Assembly.CodeBase).LocalPath);
69+
var manifestPath = Path.GetDirectoryName(typeof(ServerFactory<,>).Assembly.Location);
7070
builder.ConfigureAppConfiguration((ctx, cb) =>
7171
{
7272
if (ctx.HostingEnvironment.WebRootFileProvider is CompositeFileProvider composite)

src/Mvc/Mvc.Core/src/ApplicationParts/RelatedAssemblyAttribute.cs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ internal static IReadOnlyList<Assembly> GetRelatedAssemblies(
6666

6767
// MVC will specifically look for related parts in the same physical directory as the assembly.
6868
// No-op if the assembly does not have a location.
69-
if (assembly.IsDynamic || string.IsNullOrEmpty(assembly.CodeBase))
69+
if (assembly.IsDynamic || string.IsNullOrEmpty(assembly.Location))
7070
{
7171
return Array.Empty<Assembly>();
7272
}
@@ -78,7 +78,7 @@ internal static IReadOnlyList<Assembly> GetRelatedAssemblies(
7878
}
7979

8080
var assemblyName = assembly.GetName().Name;
81-
var assemblyLocation = GetAssemblyLocation(assembly);
81+
var assemblyLocation = assembly.Location;
8282
var assemblyDirectory = Path.GetDirectoryName(assemblyLocation);
8383

8484
var relatedAssemblies = new List<Assembly>();
@@ -112,16 +112,5 @@ internal static IReadOnlyList<Assembly> GetRelatedAssemblies(
112112

113113
return relatedAssemblies;
114114
}
115-
116-
internal static string GetAssemblyLocation(Assembly assembly)
117-
{
118-
if (Uri.TryCreate(assembly.CodeBase, UriKind.Absolute, out var result) &&
119-
result.IsFile && string.IsNullOrWhiteSpace(result.Fragment))
120-
{
121-
return result.LocalPath;
122-
}
123-
124-
return assembly.Location;
125-
}
126115
}
127116
}

src/Mvc/Mvc.Core/test/ApplicationParts/RelatedAssemblyPartTest.cs

Lines changed: 0 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -75,88 +75,6 @@ public void GetRelatedAssemblies_LoadsRelatedAssembly()
7575
Assert.Equal(new[] { relatedAssembly }, result);
7676
}
7777

78-
[Fact]
79-
public void GetAssemblyLocation_UsesCodeBase()
80-
{
81-
// Arrange
82-
var destination = Path.Combine(AssemblyDirectory, "RelatedAssembly.dll");
83-
var codeBase = "file://x:/file/Assembly.dll";
84-
var expected = new Uri(codeBase).LocalPath;
85-
var assembly = new TestAssembly
86-
{
87-
CodeBaseSettable = codeBase,
88-
};
89-
90-
// Act
91-
var actual = RelatedAssemblyAttribute.GetAssemblyLocation(assembly);
92-
Assert.Equal(expected, actual);
93-
}
94-
95-
[Fact]
96-
public void GetAssemblyLocation_UsesLocation_IfCodeBaseIsNotLocal()
97-
{
98-
// Arrange
99-
var destination = Path.Combine(AssemblyDirectory, "RelatedAssembly.dll");
100-
var expected = Path.Combine(AssemblyDirectory, "Some-Dir", "Assembly.dll");
101-
var assembly = new TestAssembly
102-
{
103-
CodeBaseSettable = "https://www.microsoft.com/test.dll",
104-
LocationSettable = expected,
105-
};
106-
107-
// Act
108-
var actual = RelatedAssemblyAttribute.GetAssemblyLocation(assembly);
109-
Assert.Equal(expected, actual);
110-
}
111-
112-
[Fact]
113-
public void GetAssemblyLocation_CodeBase_HasPoundCharacterUnixPath()
114-
{
115-
var destination = Path.Combine(AssemblyDirectory, "RelatedAssembly.dll");
116-
var expected = @"/etc/#NIN/dotnetcore/tryx/try1.dll";
117-
var assembly = new TestAssembly
118-
{
119-
CodeBaseSettable = "file:///etc/#NIN/dotnetcore/tryx/try1.dll",
120-
LocationSettable = expected,
121-
};
122-
123-
// Act
124-
var actual = RelatedAssemblyAttribute.GetAssemblyLocation(assembly);
125-
Assert.Equal(expected, actual);
126-
}
127-
128-
[Fact]
129-
public void GetAssemblyLocation_CodeBase_HasPoundCharacterUNCPath()
130-
{
131-
var destination = Path.Combine(AssemblyDirectory, "RelatedAssembly.dll");
132-
var expected = @"\\server\#NIN\dotnetcore\tryx\try1.dll";
133-
var assembly = new TestAssembly
134-
{
135-
CodeBaseSettable = "file://server/#NIN/dotnetcore/tryx/try1.dll",
136-
LocationSettable = expected,
137-
};
138-
139-
// Act
140-
var actual = RelatedAssemblyAttribute.GetAssemblyLocation(assembly);
141-
Assert.Equal(expected, actual);
142-
}
143-
144-
[Fact]
145-
public void GetAssemblyLocation_CodeBase_HasPoundCharacterDOSPath()
146-
{
147-
var destination = Path.Combine(AssemblyDirectory, "RelatedAssembly.dll");
148-
var expected = @"C:\#NIN\dotnetcore\tryx\try1.dll";
149-
var assembly = new TestAssembly
150-
{
151-
CodeBaseSettable = "file:///C:/#NIN/dotnetcore/tryx/try1.dll",
152-
LocationSettable = expected,
153-
};
154-
155-
// Act
156-
var actual = RelatedAssemblyAttribute.GetAssemblyLocation(assembly);
157-
Assert.Equal(expected, actual);
158-
}
159-
16078
private class TestAssembly : Assembly
16179
{
16280
public override AssemblyName GetName()
@@ -166,10 +84,6 @@ public override AssemblyName GetName()
16684

16785
public string AttributeAssembly { get; set; }
16886

169-
public string CodeBaseSettable { get; set; } = Path.Combine(AssemblyDirectory, "MyAssembly.dll");
170-
171-
public override string CodeBase => CodeBaseSettable;
172-
17387
public string LocationSettable { get; set; } = Path.Combine(AssemblyDirectory, "MyAssembly.dll");
17488

17589
public override string Location => LocationSettable;

src/Servers/IIS/IIS/test/IIS.Tests/Utilities/TestServer.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class TestServer: IDisposable, IStartup
3030
private const string HWebCoreDll = "hwebcore.dll";
3131

3232
internal static string HostableWebCoreLocation => Environment.ExpandEnvironmentVariables($@"%windir%\system32\inetsrv\{HWebCoreDll}");
33-
internal static string BasePath => Path.Combine(Path.GetDirectoryName(new Uri(typeof(TestServer).Assembly.CodeBase).AbsolutePath),
33+
internal static string BasePath => Path.Combine(Path.GetDirectoryName(typeof(TestServer).Assembly.Location),
3434
"ANCM",
3535
Environment.Is64BitProcess ? "x64" : "x86");
3636

@@ -45,7 +45,7 @@ public class TestServer: IDisposable, IStartup
4545
private readonly Action<IApplicationBuilder> _appBuilder;
4646
private readonly ILoggerFactory _loggerFactory;
4747
private readonly hostfxr_main_fn _hostfxrMainFn;
48-
48+
4949
private Uri BaseUri => new Uri("http://localhost:" + _currentPort);
5050
public HttpClient HttpClient { get; private set; }
5151
public TestConnection CreateConnection() => new TestConnection(_currentPort);
@@ -88,7 +88,7 @@ private void Start()
8888
{
8989
LoadLibrary(HostableWebCoreLocation);
9090
_appHostConfigPath = Path.GetTempFileName();
91-
91+
9292
set_main_handler(_hostfxrMainFn);
9393

9494
Retry(() =>
@@ -159,7 +159,7 @@ public void Dispose()
159159

160160
// WebCoreShutdown occasionally AVs
161161
// This causes the dotnet test process to crash
162-
// To avoid this, we have to wait to shutdown
162+
// To avoid this, we have to wait to shutdown
163163
// and pass in true to immediately shutdown the hostable web core
164164
// Both of these seem to be required.
165165
Thread.Sleep(100);

0 commit comments

Comments
 (0)