diff --git a/src/Microsoft.AspNetCore.Mvc.Core/Infrastructure/DefaultAssemblyProvider.cs b/src/Microsoft.AspNetCore.Mvc.Core/Infrastructure/DefaultAssemblyProvider.cs index 12cc4dc0d9..6a9da434fb 100644 --- a/src/Microsoft.AspNetCore.Mvc.Core/Infrastructure/DefaultAssemblyProvider.cs +++ b/src/Microsoft.AspNetCore.Mvc.Core/Infrastructure/DefaultAssemblyProvider.cs @@ -17,6 +17,7 @@ namespace Microsoft.AspNetCore.Mvc.Infrastructure /// public class DefaultAssemblyProvider : IAssemblyProvider { + private readonly Assembly _entryAssembly; private readonly DependencyContext _dependencyContext; /// @@ -25,8 +26,8 @@ namespace Microsoft.AspNetCore.Mvc.Infrastructure /// The . public DefaultAssemblyProvider(IApplicationEnvironment environment) { - var applicationAssembly = Assembly.Load(new AssemblyName(environment.ApplicationName)); - _dependencyContext = DependencyContext.Load(applicationAssembly); + _entryAssembly = Assembly.Load(new AssemblyName(environment.ApplicationName)); + _dependencyContext = DependencyContext.Load(_entryAssembly); } /// @@ -59,6 +60,12 @@ namespace Microsoft.AspNetCore.Mvc.Infrastructure { get { + if (_dependencyContext == null) + { + // Use the entry assembly as the sole candidate. + return new[] { _entryAssembly }; + } + return GetCandidateLibraries() .SelectMany(l => l.Assemblies) .Select(Load); @@ -78,7 +85,7 @@ namespace Microsoft.AspNetCore.Mvc.Infrastructure return Enumerable.Empty(); } - return DependencyContext.Default.RuntimeLibraries.Where(IsCandidateLibrary); + return _dependencyContext.RuntimeLibraries.Where(IsCandidateLibrary); } private static Assembly Load(RuntimeAssembly assembly) diff --git a/src/Microsoft.AspNetCore.Mvc.Razor/Internal/DefaultRoslynCompilationService.cs b/src/Microsoft.AspNetCore.Mvc.Razor/Internal/DefaultRoslynCompilationService.cs index ea82220310..1173c54336 100644 --- a/src/Microsoft.AspNetCore.Mvc.Razor/Internal/DefaultRoslynCompilationService.cs +++ b/src/Microsoft.AspNetCore.Mvc.Razor/Internal/DefaultRoslynCompilationService.cs @@ -243,6 +243,12 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal private List GetApplicationReferences() { + if (_dependencyContext == null) + { + // Avoid null ref if the entry point does not have DependencyContext specified. + return new List(); + } + return _dependencyContext.CompileLibraries .SelectMany(library => library.ResolveReferencePaths()) .Select(CreateMetadataFileReference)