Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Commit

Permalink
Use dependency context from all application parts when compiling views
Browse files Browse the repository at this point in the history
  • Loading branch information
pranavkm committed May 2, 2016
1 parent 0ff2f87 commit b3618f4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.Threading;
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.ApplicationParts;
using Microsoft.AspNetCore.Mvc.Razor.Compilation;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
Expand All @@ -29,7 +30,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal
/// </summary>
public class DefaultRoslynCompilationService : ICompilationService
{
private readonly IHostingEnvironment _hostingEnvironment;
private readonly ApplicationPartManager _partManager;
private readonly IFileProvider _fileProvider;
private readonly Action<RoslynCompilationContext> _compilationCallback;
private readonly CSharpParseOptions _parseOptions;
Expand All @@ -42,17 +43,17 @@ public class DefaultRoslynCompilationService : ICompilationService
/// <summary>
/// Initalizes a new instance of the <see cref="DefaultRoslynCompilationService"/> class.
/// </summary>
/// <param name="environment">The <see cref="IHostingEnvironment"/>.</param>
/// <param name="partManager">The <see cref="ApplicationPartManager"/>.</param>
/// <param name="optionsAccessor">Accessor to <see cref="RazorViewEngineOptions"/>.</param>
/// <param name="fileProviderAccessor">The <see cref="IRazorViewEngineFileProviderAccessor"/>.</param>
/// <param name="loggerFactory">The <see cref="ILoggerFactory"/>.</param>
public DefaultRoslynCompilationService(
IHostingEnvironment environment,
ApplicationPartManager partManager,
IOptions<RazorViewEngineOptions> optionsAccessor,
IRazorViewEngineFileProviderAccessor fileProviderAccessor,
ILoggerFactory loggerFactory)
{
_hostingEnvironment = environment;
_partManager = partManager;
_fileProvider = fileProviderAccessor.FileProvider;
_compilationCallback = optionsAccessor.Value.CompilationCallback;
_parseOptions = optionsAccessor.Value.ParseOptions;
Expand Down Expand Up @@ -154,17 +155,16 @@ public CompilationResult Compile(RelativeFileInfo fileInfo, string compilationCo
/// <summary>
/// Gets the <see cref="DependencyContext"/>.
/// </summary>
/// <param name="hostingEnvironment">The <see cref="IHostingEnvironment"/>.</param>
/// <param name="assembly">The <see cref="Assembly"/> to load the <see cref="DependencyContext"/> for.</param>
/// <returns>The <see cref="DependencyContext"/>.</returns>
protected virtual DependencyContext GetDependencyContext(IHostingEnvironment hostingEnvironment)
protected virtual DependencyContext GetDependencyContext(Assembly assembly)
{
if (hostingEnvironment.ApplicationName != null)
if (assembly == null)
{
var applicationAssembly = Assembly.Load(new AssemblyName(hostingEnvironment.ApplicationName));
return DependencyContext.Load(applicationAssembly);
throw new ArgumentNullException(nameof(assembly));
}

return null;
return DependencyContext.Load(assembly);
}

private Assembly LoadStream(MemoryStream assemblyStream, MemoryStream pdbStream)
Expand Down Expand Up @@ -243,32 +243,30 @@ private static string GetFilePath(string relativePath, Diagnostic diagnostic)
private List<MetadataReference> GetApplicationReferences()
{
var metadataReferences = new List<MetadataReference>();
var dependencyContext = GetDependencyContext(_hostingEnvironment);
if (dependencyContext == null)
foreach (var part in _partManager.ApplicationParts.OfType<AssemblyPart>())
{
// Avoid null ref if the entry point does not have DependencyContext specified.
return metadataReferences;
}
var dependencyContext = GetDependencyContext(part.Assembly);

var libraryPaths = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
for (var i = 0; i < dependencyContext.CompileLibraries.Count; i++)
{
var library = dependencyContext.CompileLibraries[i];
IEnumerable<string> referencePaths;
try
var libraryPaths = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
for (var i = 0; i < dependencyContext.CompileLibraries.Count; i++)
{
referencePaths = library.ResolveReferencePaths();
}
catch (InvalidOperationException)
{
continue;
}
var library = dependencyContext.CompileLibraries[i];
IEnumerable<string> referencePaths;
try
{
referencePaths = library.ResolveReferencePaths();
}
catch (InvalidOperationException)
{
continue;
}

foreach (var path in referencePaths)
{
if (libraryPaths.Add(path))
foreach (var path in referencePaths)
{
metadataReferences.Add(CreateMetadataFileReference(path));
if (libraryPaths.Add(path))
{
metadataReferences.Add(CreateMetadataFileReference(path));
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System;
using System.Linq;
using System.Reflection;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.ApplicationParts;
using Microsoft.AspNetCore.Mvc.Razor.Compilation;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;
Expand Down Expand Up @@ -423,7 +423,7 @@ public TestableRoslynCompilationService(
RazorViewEngineOptions viewEngineOptions,
IRazorViewEngineFileProviderAccessor fileProviderAccessor)
: base(
Mock.Of<IHostingEnvironment>(),
GetManager(),
GetAccessor(viewEngineOptions),
fileProviderAccessor,
NullLoggerFactory.Instance)
Expand All @@ -438,7 +438,16 @@ private static IOptions<RazorViewEngineOptions> GetAccessor(RazorViewEngineOptio
return optionsAccessor.Object;
}

protected override DependencyContext GetDependencyContext(IHostingEnvironment hostingEnvironment)
private static ApplicationPartManager GetManager()
{
var applicationPartManager = new ApplicationPartManager();
var assembly = typeof(DefaultRoslynCompilationServiceTest).GetTypeInfo().Assembly;
applicationPartManager.ApplicationParts.Add(new AssemblyPart(assembly));

return applicationPartManager;
}

protected override DependencyContext GetDependencyContext(Assembly assembly)
=> _dependencyContext;
}
}
Expand Down

0 comments on commit b3618f4

Please sign in to comment.