From b3618f4b1c5e01b681f8dacac8a8f6d47553626e Mon Sep 17 00:00:00 2001 From: Pranav K Date: Mon, 2 May 2016 10:05:11 -0700 Subject: [PATCH] Use dependency context from all application parts when compiling views --- .../DefaultRoslynCompilationService.cs | 60 +++++++++---------- .../DefaultRoslynCompilationServiceTest.cs | 15 ++++- 2 files changed, 41 insertions(+), 34 deletions(-) diff --git a/src/Microsoft.AspNetCore.Mvc.Razor/Internal/DefaultRoslynCompilationService.cs b/src/Microsoft.AspNetCore.Mvc.Razor/Internal/DefaultRoslynCompilationService.cs index b886d11b41..1cbef52d05 100644 --- a/src/Microsoft.AspNetCore.Mvc.Razor/Internal/DefaultRoslynCompilationService.cs +++ b/src/Microsoft.AspNetCore.Mvc.Razor/Internal/DefaultRoslynCompilationService.cs @@ -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; @@ -29,7 +30,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal /// public class DefaultRoslynCompilationService : ICompilationService { - private readonly IHostingEnvironment _hostingEnvironment; + private readonly ApplicationPartManager _partManager; private readonly IFileProvider _fileProvider; private readonly Action _compilationCallback; private readonly CSharpParseOptions _parseOptions; @@ -42,17 +43,17 @@ public class DefaultRoslynCompilationService : ICompilationService /// /// Initalizes a new instance of the class. /// - /// The . + /// The . /// Accessor to . /// The . /// The . public DefaultRoslynCompilationService( - IHostingEnvironment environment, + ApplicationPartManager partManager, IOptions optionsAccessor, IRazorViewEngineFileProviderAccessor fileProviderAccessor, ILoggerFactory loggerFactory) { - _hostingEnvironment = environment; + _partManager = partManager; _fileProvider = fileProviderAccessor.FileProvider; _compilationCallback = optionsAccessor.Value.CompilationCallback; _parseOptions = optionsAccessor.Value.ParseOptions; @@ -154,17 +155,16 @@ public CompilationResult Compile(RelativeFileInfo fileInfo, string compilationCo /// /// Gets the . /// - /// The . + /// The to load the for. /// The . - 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) @@ -243,32 +243,30 @@ private static string GetFilePath(string relativePath, Diagnostic diagnostic) private List GetApplicationReferences() { var metadataReferences = new List(); - var dependencyContext = GetDependencyContext(_hostingEnvironment); - if (dependencyContext == null) + foreach (var part in _partManager.ApplicationParts.OfType()) { - // Avoid null ref if the entry point does not have DependencyContext specified. - return metadataReferences; - } + var dependencyContext = GetDependencyContext(part.Assembly); - var libraryPaths = new HashSet(StringComparer.OrdinalIgnoreCase); - for (var i = 0; i < dependencyContext.CompileLibraries.Count; i++) - { - var library = dependencyContext.CompileLibraries[i]; - IEnumerable referencePaths; - try + var libraryPaths = new HashSet(StringComparer.OrdinalIgnoreCase); + for (var i = 0; i < dependencyContext.CompileLibraries.Count; i++) { - referencePaths = library.ResolveReferencePaths(); - } - catch (InvalidOperationException) - { - continue; - } + var library = dependencyContext.CompileLibraries[i]; + IEnumerable 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)); + } } } } diff --git a/test/Microsoft.AspNetCore.Mvc.Razor.Test/Internal/DefaultRoslynCompilationServiceTest.cs b/test/Microsoft.AspNetCore.Mvc.Razor.Test/Internal/DefaultRoslynCompilationServiceTest.cs index 41abdb501e..44416aa5de 100644 --- a/test/Microsoft.AspNetCore.Mvc.Razor.Test/Internal/DefaultRoslynCompilationServiceTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Razor.Test/Internal/DefaultRoslynCompilationServiceTest.cs @@ -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; @@ -423,7 +423,7 @@ public TestableRoslynCompilationService( RazorViewEngineOptions viewEngineOptions, IRazorViewEngineFileProviderAccessor fileProviderAccessor) : base( - Mock.Of(), + GetManager(), GetAccessor(viewEngineOptions), fileProviderAccessor, NullLoggerFactory.Instance) @@ -438,7 +438,16 @@ private static IOptions 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; } }