diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextLoader.cs b/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextLoader.cs index fc94a2cac6ae8d..a411055df2c5c8 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextLoader.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextLoader.cs @@ -127,7 +127,15 @@ private DependencyContext LoadAssemblyContext(Assembly assembly, IDependencyCont private string GetDepsJsonPath(Assembly assembly) { - string depsJsonFile = Path.ChangeExtension(assembly.Location, DepsJsonExtension); + // Assemblies loaded in memory (e.g. single file) return empty string from Location. + // In these cases, don't try probing next to the assembly. + string assemblyLocation = assembly.Location; + if (string.IsNullOrEmpty(assemblyLocation)) + { + return null; + } + + string depsJsonFile = Path.ChangeExtension(assemblyLocation, DepsJsonExtension); bool depsJsonFileExists = _fileSystem.File.Exists(depsJsonFile); if (!depsJsonFileExists) @@ -136,7 +144,7 @@ private string GetDepsJsonPath(Assembly assembly) // and CodeBase will be different, so also try the CodeBase string assemblyCodeBase = GetNormalizedCodeBasePath(assembly); if (!string.IsNullOrEmpty(assemblyCodeBase) && - assembly.Location != assemblyCodeBase) + assemblyLocation != assemblyCodeBase) { depsJsonFile = Path.ChangeExtension(assemblyCodeBase, DepsJsonExtension); depsJsonFileExists = _fileSystem.File.Exists(depsJsonFile); diff --git a/src/libraries/Microsoft.Extensions.DependencyModel/tests/DependencyContextLoaderTests.cs b/src/libraries/Microsoft.Extensions.DependencyModel/tests/DependencyContextLoaderTests.cs index 8e8070e8a51357..e7e5ce6ecb8167 100644 --- a/src/libraries/Microsoft.Extensions.DependencyModel/tests/DependencyContextLoaderTests.cs +++ b/src/libraries/Microsoft.Extensions.DependencyModel/tests/DependencyContextLoaderTests.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using FluentAssertions; +using System.IO; using System.Reflection; using Xunit; @@ -90,5 +91,19 @@ public void LoadReturnsNullWhenNotFound() var loader = new DependencyContextLoader(); Assert.Null(loader.Load(typeof(Moq.Mock).Assembly)); } + + [Fact] + public void LoadReturnsNullWhenAssemblyLocationIsEmpty() + { + var loader = new DependencyContextLoader(); + Assert.Null(loader.Load(new EmptyLocationAssembly())); + } + + private class EmptyLocationAssembly : Assembly + { + public override string Location => string.Empty; + public override AssemblyName GetName() => new AssemblyName("EmptyLocation"); + public override Stream? GetManifestResourceStream(string name) => null; + } } }