Use the entry assembly when DependencyContext is null

Fixes #4136
This commit is contained in:
Pranav K 2016-02-24 21:43:36 -08:00
parent 140c8686b5
commit 756953fd50
2 changed files with 16 additions and 3 deletions

View File

@ -17,6 +17,7 @@ namespace Microsoft.AspNetCore.Mvc.Infrastructure
/// </summary>
public class DefaultAssemblyProvider : IAssemblyProvider
{
private readonly Assembly _entryAssembly;
private readonly DependencyContext _dependencyContext;
/// <summary>
@ -25,8 +26,8 @@ namespace Microsoft.AspNetCore.Mvc.Infrastructure
/// <param name="environment">The <see cref="IApplicationEnvironment"/>.</param>
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);
}
/// <summary>
@ -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<RuntimeLibrary>();
}
return DependencyContext.Default.RuntimeLibraries.Where(IsCandidateLibrary);
return _dependencyContext.RuntimeLibraries.Where(IsCandidateLibrary);
}
private static Assembly Load(RuntimeAssembly assembly)

View File

@ -243,6 +243,12 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal
private List<MetadataReference> GetApplicationReferences()
{
if (_dependencyContext == null)
{
// Avoid null ref if the entry point does not have DependencyContext specified.
return new List<MetadataReference>();
}
return _dependencyContext.CompileLibraries
.SelectMany(library => library.ResolveReferencePaths())
.Select(CreateMetadataFileReference)