Revert "Revert DependencyContext changes untill dotnet-cli packages get sorted out"
This reverts commit 57bf12311b.
This commit is contained in:
parent
420f442487
commit
aa5a4d4af2
|
|
@ -78,7 +78,15 @@ namespace Microsoft.Extensions.DependencyInjection
|
|||
// Action Discovery
|
||||
//
|
||||
// These are consumed only when creating action descriptors, then they can be de-allocated
|
||||
services.TryAddTransient<IAssemblyProvider, DefaultAssemblyProvider>();
|
||||
if (PlatformServices.Default?.LibraryManager != null)
|
||||
{
|
||||
services.TryAddTransient<IAssemblyProvider, DefaultAssemblyProvider>();
|
||||
}
|
||||
else
|
||||
{
|
||||
services.TryAddTransient<IAssemblyProvider, DependencyContextAssemblyProvider>();
|
||||
}
|
||||
|
||||
services.TryAddTransient<IControllerTypeProvider, DefaultControllerTypeProvider>();
|
||||
services.TryAddEnumerable(
|
||||
ServiceDescriptor.Transient<IApplicationModelProvider, DefaultApplicationModelProvider>());
|
||||
|
|
|
|||
|
|
@ -41,8 +41,7 @@ namespace Microsoft.AspNet.Mvc.Infrastructure
|
|||
"Microsoft.AspNet.Mvc.Razor",
|
||||
"Microsoft.AspNet.Mvc.Razor.Host",
|
||||
"Microsoft.AspNet.Mvc.TagHelpers",
|
||||
"Microsoft.AspNet.Mvc.ViewFeatures",
|
||||
"Microsoft.AspNet.PageExecutionInstrumentation.Interfaces",
|
||||
"Microsoft.AspNet.Mvc.ViewFeatures"
|
||||
};
|
||||
|
||||
/// <inheritdoc />
|
||||
|
|
|
|||
|
|
@ -0,0 +1,78 @@
|
|||
// Copyright (c) .NET Foundation. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using Microsoft.Extensions.DependencyModel;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.Infrastructure
|
||||
{
|
||||
public class DependencyContextAssemblyProvider : IAssemblyProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the set of assembly names that are used as root for discovery of
|
||||
/// MVC controllers, view components and views.
|
||||
/// </summary>
|
||||
// DefaultControllerTypeProvider uses CandidateAssemblies to determine if the base type of a POCO controller
|
||||
// lives in an assembly that references MVC. CandidateAssemblies excludes all assemblies from the
|
||||
// ReferenceAssemblies set. Consequently adding WebApiCompatShim to this set would cause the ApiController to
|
||||
// fail this test.
|
||||
protected virtual HashSet<string> ReferenceAssemblies { get; } = new HashSet<string>(StringComparer.Ordinal)
|
||||
{
|
||||
"Microsoft.AspNet.Mvc",
|
||||
"Microsoft.AspNet.Mvc.Abstractions",
|
||||
"Microsoft.AspNet.Mvc.ApiExplorer",
|
||||
"Microsoft.AspNet.Mvc.Core",
|
||||
"Microsoft.AspNet.Mvc.Cors",
|
||||
"Microsoft.AspNet.Mvc.DataAnnotations",
|
||||
"Microsoft.AspNet.Mvc.Formatters.Json",
|
||||
"Microsoft.AspNet.Mvc.Formatters.Xml",
|
||||
"Microsoft.AspNet.Mvc.Localization",
|
||||
"Microsoft.AspNet.Mvc.Razor",
|
||||
"Microsoft.AspNet.Mvc.Razor.Host",
|
||||
"Microsoft.AspNet.Mvc.TagHelpers",
|
||||
"Microsoft.AspNet.Mvc.ViewFeatures"
|
||||
};
|
||||
|
||||
/// <inheritdoc />
|
||||
public IEnumerable<Assembly> CandidateAssemblies
|
||||
{
|
||||
get
|
||||
{
|
||||
return GetCandidateLibraries()
|
||||
.SelectMany(l => l.Assemblies)
|
||||
.Select(Load);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a list of libraries that references the assemblies in <see cref="ReferenceAssemblies"/>.
|
||||
/// By default it returns all assemblies that reference any of the primary MVC assemblies
|
||||
/// while ignoring MVC assemblies.
|
||||
/// </summary>
|
||||
/// <returns>A set of <see cref="Library"/>.</returns>
|
||||
protected virtual IEnumerable<RuntimeLibrary> GetCandidateLibraries()
|
||||
{
|
||||
if (ReferenceAssemblies == null)
|
||||
{
|
||||
return Enumerable.Empty<RuntimeLibrary>();
|
||||
}
|
||||
|
||||
return DependencyContext.Default.RuntimeLibraries.Where(IsCandidateLibrary);
|
||||
}
|
||||
|
||||
private static Assembly Load(RuntimeAssembly assembly)
|
||||
{
|
||||
return Assembly.Load(assembly.Name);
|
||||
}
|
||||
|
||||
private bool IsCandidateLibrary(RuntimeLibrary library)
|
||||
{
|
||||
Debug.Assert(ReferenceAssemblies != null);
|
||||
return library.Dependencies.Any(dependency => ReferenceAssemblies.Contains(dependency.Name));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -21,6 +21,7 @@
|
|||
"version": "1.0.0-*"
|
||||
},
|
||||
"Microsoft.Extensions.PlatformAbstractions": "1.0.0-*",
|
||||
"Microsoft.Extensions.DependencyModel": "1.0.0-*",
|
||||
"Microsoft.Extensions.ClosedGenericMatcher.Sources": {
|
||||
"version": "1.0.0-*",
|
||||
"type": "build"
|
||||
|
|
@ -48,6 +49,7 @@
|
|||
"frameworks": {
|
||||
"net451": {
|
||||
"frameworkAssemblies": {
|
||||
"System.Reflection": "",
|
||||
"System.Runtime": ""
|
||||
}
|
||||
},
|
||||
|
|
|
|||
|
|
@ -0,0 +1,121 @@
|
|||
// Copyright (c) .NET Foundation. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.Dnx.Compilation.CSharp;
|
||||
using Microsoft.Extensions.CompilationAbstractions;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Microsoft.Extensions.PlatformAbstractions;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.Razor.Compilation
|
||||
{
|
||||
/// <summary>
|
||||
/// A type that uses Roslyn to compile C# content and <see cref="ILibraryExporter"/> to find out references.
|
||||
/// </summary>
|
||||
public class DefaultRoslynCompilationService : RoslynCompilationService
|
||||
{
|
||||
private readonly IApplicationEnvironment _environment;
|
||||
private readonly ILibraryExporter _libraryExporter;
|
||||
|
||||
/// <summary>
|
||||
/// Initalizes a new instance of the <see cref="DefaultRoslynCompilationService"/> class.
|
||||
/// </summary>
|
||||
/// <param name="environment">The environment for the executing application.</param>
|
||||
/// <param name="libraryExporter">The library manager that provides export and reference information.</param>
|
||||
/// <param name="host">The <see cref="IMvcRazorHost"/> that was used to generate the code.</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(IApplicationEnvironment environment,
|
||||
ILibraryExporter libraryExporter,
|
||||
IMvcRazorHost host,
|
||||
IOptions<RazorViewEngineOptions> optionsAccessor,
|
||||
IRazorViewEngineFileProviderAccessor fileProviderAccessor,
|
||||
ILoggerFactory loggerFactory)
|
||||
: base(environment, host, optionsAccessor, fileProviderAccessor, loggerFactory)
|
||||
{
|
||||
_environment = environment;
|
||||
_libraryExporter = libraryExporter;
|
||||
}
|
||||
|
||||
protected override List<MetadataReference> GetApplicationReferences()
|
||||
{
|
||||
var references = new List<MetadataReference>();
|
||||
|
||||
// Get the MetadataReference for the executing application. If it's a Roslyn reference,
|
||||
// we can copy the references created when compiling the application to the Razor page being compiled.
|
||||
// This avoids performing expensive calls to MetadataReference.CreateFromImage.
|
||||
var libraryExport = _libraryExporter.GetExport(_environment.ApplicationName);
|
||||
if (libraryExport?.MetadataReferences != null && libraryExport.MetadataReferences.Count > 0)
|
||||
{
|
||||
Debug.Assert(libraryExport.MetadataReferences.Count == 1,
|
||||
"Expected 1 MetadataReferences, found " + libraryExport.MetadataReferences.Count);
|
||||
var roslynReference = libraryExport.MetadataReferences[0] as IRoslynMetadataReference;
|
||||
var compilationReference = roslynReference?.MetadataReference as CompilationReference;
|
||||
if (compilationReference != null)
|
||||
{
|
||||
references.AddRange(compilationReference.Compilation.References);
|
||||
references.Add(roslynReference.MetadataReference);
|
||||
return references;
|
||||
}
|
||||
}
|
||||
|
||||
var export = _libraryExporter.GetAllExports(_environment.ApplicationName);
|
||||
if (export != null)
|
||||
{
|
||||
foreach (var metadataReference in export.MetadataReferences)
|
||||
{
|
||||
// Taken from https://github.com/aspnet/KRuntime/blob/757ba9bfdf80bd6277e715d6375969a7f44370ee/src/...
|
||||
// Microsoft.Extensions.Runtime.Roslyn/RoslynCompiler.cs#L164
|
||||
// We don't want to take a dependency on the Roslyn bit directly since it pulls in more dependencies
|
||||
// than the view engine needs (Microsoft.Extensions.Runtime) for example
|
||||
references.Add(ConvertMetadataReference(metadataReference));
|
||||
}
|
||||
}
|
||||
|
||||
return references;
|
||||
}
|
||||
|
||||
private MetadataReference ConvertMetadataReference(IMetadataReference metadataReference)
|
||||
{
|
||||
var roslynReference = metadataReference as IRoslynMetadataReference;
|
||||
|
||||
if (roslynReference != null)
|
||||
{
|
||||
return roslynReference.MetadataReference;
|
||||
}
|
||||
|
||||
var embeddedReference = metadataReference as IMetadataEmbeddedReference;
|
||||
|
||||
if (embeddedReference != null)
|
||||
{
|
||||
return MetadataReference.CreateFromImage(embeddedReference.Contents);
|
||||
}
|
||||
|
||||
var fileMetadataReference = metadataReference as IMetadataFileReference;
|
||||
|
||||
if (fileMetadataReference != null)
|
||||
{
|
||||
return CreateMetadataFileReference(fileMetadataReference.Path);
|
||||
}
|
||||
|
||||
var projectReference = metadataReference as IMetadataProjectReference;
|
||||
if (projectReference != null)
|
||||
{
|
||||
using (var ms = new MemoryStream())
|
||||
{
|
||||
projectReference.EmitReferenceAssembly(ms);
|
||||
|
||||
return MetadataReference.CreateFromImage(ms.ToArray());
|
||||
}
|
||||
}
|
||||
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,68 @@
|
|||
// Copyright (c) .NET Foundation. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.Extensions.DependencyModel;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Microsoft.Extensions.PlatformAbstractions;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.Razor.Compilation
|
||||
{
|
||||
/// <summary>
|
||||
/// A type that uses Roslyn to compile C# content and <see cref="DependencyContext"/> to locate references.
|
||||
/// </summary>
|
||||
public class DependencyContextCompilationService : RoslynCompilationService
|
||||
{
|
||||
private readonly DependencyContext _dependencyContext;
|
||||
|
||||
/// <summary>
|
||||
/// Initalizes a new instance of the <see cref="DependencyContextCompilationService"/> class.
|
||||
/// </summary>
|
||||
/// <param name="environment">The environment for the executing application.</param>
|
||||
/// <param name="host">The <see cref="IMvcRazorHost"/> that was used to generate the code.</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 DependencyContextCompilationService(
|
||||
IApplicationEnvironment environment,
|
||||
IMvcRazorHost host,
|
||||
IOptions<RazorViewEngineOptions> optionsAccessor,
|
||||
IRazorViewEngineFileProviderAccessor fileProviderAccessor,
|
||||
ILoggerFactory loggerFactory)
|
||||
: this(DependencyContext.Default, environment, host, optionsAccessor, fileProviderAccessor, loggerFactory)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initalizes a new instance of the <see cref="DependencyContextCompilationService"/> class.
|
||||
/// </summary>
|
||||
/// <param name="dependencyContext"><see cref="DependencyContext"/> to use for reference resolution.</param>
|
||||
/// <param name="environment">The environment for the executing application.</param>
|
||||
/// <param name="host">The <see cref="IMvcRazorHost"/> that was used to generate the code.</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 DependencyContextCompilationService(
|
||||
DependencyContext dependencyContext,
|
||||
IApplicationEnvironment environment,
|
||||
IMvcRazorHost host,
|
||||
IOptions<RazorViewEngineOptions> optionsAccessor,
|
||||
IRazorViewEngineFileProviderAccessor fileProviderAccessor,
|
||||
ILoggerFactory loggerFactory)
|
||||
: base(environment, host, optionsAccessor, fileProviderAccessor, loggerFactory)
|
||||
{
|
||||
_dependencyContext = dependencyContext;
|
||||
}
|
||||
|
||||
protected override List<MetadataReference> GetApplicationReferences()
|
||||
{
|
||||
return _dependencyContext.CompileLibraries
|
||||
.SelectMany(library => library.ResolveReferencePaths())
|
||||
.Select(CreateMetadataFileReference)
|
||||
.ToList();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -13,6 +13,7 @@ using System.Reflection.PortableExecutable;
|
|||
using System.Runtime.Loader;
|
||||
#endif
|
||||
using System.Runtime.Versioning;
|
||||
using Microsoft.AspNet.Diagnostics;
|
||||
using Microsoft.AspNet.FileProviders;
|
||||
using Microsoft.AspNet.Mvc.Logging;
|
||||
using Microsoft.AspNet.Mvc.Razor.Internal;
|
||||
|
|
@ -22,7 +23,6 @@ using Microsoft.CodeAnalysis.Emit;
|
|||
using Microsoft.Dnx.Compilation.CSharp;
|
||||
using Microsoft.Extensions.PlatformAbstractions;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Microsoft.AspNet.Diagnostics;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.Razor.Compilation
|
||||
|
|
@ -30,13 +30,12 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation
|
|||
/// <summary>
|
||||
/// A type that uses Roslyn to compile C# content.
|
||||
/// </summary>
|
||||
public class RoslynCompilationService : ICompilationService
|
||||
public abstract class RoslynCompilationService : ICompilationService
|
||||
{
|
||||
private readonly Lazy<bool> _supportsPdbGeneration = new Lazy<bool>(SymbolsUtility.SupportsSymbolsGeneration);
|
||||
private readonly ConcurrentDictionary<string, AssemblyMetadata> _metadataFileCache =
|
||||
new ConcurrentDictionary<string, AssemblyMetadata>(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
private readonly Extensions.CompilationAbstractions.ILibraryExporter _libraryExporter;
|
||||
private readonly IApplicationEnvironment _environment;
|
||||
private readonly IFileProvider _fileProvider;
|
||||
private readonly Lazy<List<MetadataReference>> _applicationReferences;
|
||||
|
|
@ -54,20 +53,17 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation
|
|||
/// Initalizes a new instance of the <see cref="RoslynCompilationService"/> class.
|
||||
/// </summary>
|
||||
/// <param name="environment">The environment for the executing application.</param>
|
||||
/// <param name="libraryExporter">The library manager that provides export and reference information.</param>
|
||||
/// <param name="host">The <see cref="IMvcRazorHost"/> that was used to generate the code.</param>
|
||||
/// <param name="optionsAccessor">Accessor to <see cref="RazorViewEngineOptions"/>.</param>
|
||||
/// <param name="fileProviderAccessor">The <see cref="IRazorViewEngineFileProviderAccessor"/>.</param>
|
||||
public RoslynCompilationService(
|
||||
IApplicationEnvironment environment,
|
||||
Extensions.CompilationAbstractions.ILibraryExporter libraryExporter,
|
||||
IMvcRazorHost host,
|
||||
IOptions<RazorViewEngineOptions> optionsAccessor,
|
||||
IRazorViewEngineFileProviderAccessor fileProviderAccessor,
|
||||
ILoggerFactory loggerFactory)
|
||||
{
|
||||
_environment = environment;
|
||||
_libraryExporter = libraryExporter;
|
||||
_applicationReferences = new Lazy<List<MetadataReference>>(GetApplicationReferences);
|
||||
_fileProvider = fileProviderAccessor.FileProvider;
|
||||
_classPrefix = host.MainClassNamePrefix;
|
||||
|
|
@ -156,8 +152,9 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation
|
|||
assembly = LoadStream(ms, assemblySymbols: null);
|
||||
}
|
||||
|
||||
var type = assembly.GetExportedTypes()
|
||||
.First(t => t.Name.StartsWith(_classPrefix, StringComparison.Ordinal));
|
||||
var type = assembly
|
||||
.GetExportedTypes()
|
||||
.First(t => t.Name.StartsWith(_classPrefix, StringComparison.Ordinal));
|
||||
|
||||
_logger.GeneratedCodeToAssemblyCompilationEnd(fileInfo.RelativePath, startTimestamp);
|
||||
|
||||
|
|
@ -239,83 +236,9 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation
|
|||
return diagnostic.Location.GetMappedLineSpan().Path;
|
||||
}
|
||||
|
||||
private List<MetadataReference> GetApplicationReferences()
|
||||
{
|
||||
var references = new List<MetadataReference>();
|
||||
protected abstract List<MetadataReference> GetApplicationReferences();
|
||||
|
||||
// Get the MetadataReference for the executing application. If it's a Roslyn reference,
|
||||
// we can copy the references created when compiling the application to the Razor page being compiled.
|
||||
// This avoids performing expensive calls to MetadataReference.CreateFromImage.
|
||||
var libraryExport = _libraryExporter.GetExport(_environment.ApplicationName);
|
||||
if (libraryExport?.MetadataReferences != null && libraryExport.MetadataReferences.Count > 0)
|
||||
{
|
||||
Debug.Assert(libraryExport.MetadataReferences.Count == 1,
|
||||
"Expected 1 MetadataReferences, found " + libraryExport.MetadataReferences.Count);
|
||||
var roslynReference = libraryExport.MetadataReferences[0] as IRoslynMetadataReference;
|
||||
var compilationReference = roslynReference?.MetadataReference as CompilationReference;
|
||||
if (compilationReference != null)
|
||||
{
|
||||
references.AddRange(compilationReference.Compilation.References);
|
||||
references.Add(roslynReference.MetadataReference);
|
||||
return references;
|
||||
}
|
||||
}
|
||||
|
||||
var export = _libraryExporter.GetAllExports(_environment.ApplicationName);
|
||||
if (export != null)
|
||||
{
|
||||
foreach (var metadataReference in export.MetadataReferences)
|
||||
{
|
||||
// Taken from https://github.com/aspnet/KRuntime/blob/757ba9bfdf80bd6277e715d6375969a7f44370ee/src/...
|
||||
// Microsoft.Extensions.Runtime.Roslyn/RoslynCompiler.cs#L164
|
||||
// We don't want to take a dependency on the Roslyn bit directly since it pulls in more dependencies
|
||||
// than the view engine needs (Microsoft.Extensions.Runtime) for example
|
||||
references.Add(ConvertMetadataReference(metadataReference));
|
||||
}
|
||||
}
|
||||
|
||||
return references;
|
||||
}
|
||||
|
||||
private MetadataReference ConvertMetadataReference(
|
||||
Extensions.CompilationAbstractions.IMetadataReference metadataReference)
|
||||
{
|
||||
var roslynReference = metadataReference as IRoslynMetadataReference;
|
||||
|
||||
if (roslynReference != null)
|
||||
{
|
||||
return roslynReference.MetadataReference;
|
||||
}
|
||||
|
||||
var embeddedReference = metadataReference as Extensions.CompilationAbstractions.IMetadataEmbeddedReference;
|
||||
|
||||
if (embeddedReference != null)
|
||||
{
|
||||
return MetadataReference.CreateFromImage(embeddedReference.Contents);
|
||||
}
|
||||
|
||||
var fileMetadataReference = metadataReference as Extensions.CompilationAbstractions.IMetadataFileReference;
|
||||
|
||||
if (fileMetadataReference != null)
|
||||
{
|
||||
return CreateMetadataFileReference(fileMetadataReference.Path);
|
||||
}
|
||||
|
||||
var projectReference = metadataReference as Extensions.CompilationAbstractions.IMetadataProjectReference;
|
||||
if (projectReference != null)
|
||||
{
|
||||
using (var ms = new MemoryStream())
|
||||
{
|
||||
projectReference.EmitReferenceAssembly(ms);
|
||||
|
||||
return MetadataReference.CreateFromImage(ms.ToArray());
|
||||
}
|
||||
}
|
||||
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
private MetadataReference CreateMetadataFileReference(string path)
|
||||
protected MetadataReference CreateMetadataFileReference(string path)
|
||||
{
|
||||
var metadata = _metadataFileCache.GetOrAdd(path, _ =>
|
||||
{
|
||||
|
|
|
|||
|
|
@ -0,0 +1,88 @@
|
|||
// Copyright (c) .NET Foundation. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNet.Mvc.Razor;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Microsoft.Extensions.DependencyModel;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc
|
||||
{
|
||||
/// <summary>
|
||||
/// Sets up compilation and parse option default options for <see cref="RazorViewEngineOptions"/> using <see cref="DependencyContext"/>
|
||||
/// </summary>
|
||||
public class DependencyContextRazorViewEngineOptionsSetup : ConfigureOptions<RazorViewEngineOptions>
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of <see cref="DependencyContextRazorViewEngineOptionsSetup"/>.
|
||||
/// </summary>
|
||||
public DependencyContextRazorViewEngineOptionsSetup() : this(DependencyContext.Default)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of <see cref="DependencyContextRazorViewEngineOptionsSetup"/>.
|
||||
/// </summary>
|
||||
/// <param name="dependencyContext"><see cref="DependencyContext"/> to use as compilation and parse option source.</param>
|
||||
public DependencyContextRazorViewEngineOptionsSetup(DependencyContext dependencyContext) : base(options => ConfigureRazor(options, dependencyContext))
|
||||
{
|
||||
}
|
||||
|
||||
private static void ConfigureRazor(RazorViewEngineOptions options, DependencyContext dependencyContext)
|
||||
{
|
||||
var compilationOptions = dependencyContext.CompilationOptions;
|
||||
|
||||
SetParseOptions(options, compilationOptions);
|
||||
SetCompilationOptions(options, compilationOptions);
|
||||
}
|
||||
|
||||
private static void SetCompilationOptions(RazorViewEngineOptions options, Microsoft.Extensions.DependencyModel.CompilationOptions compilationOptions)
|
||||
{
|
||||
var roslynOptions = options.CompilationOptions;
|
||||
|
||||
// Disable 1702 until roslyn turns this off by default
|
||||
roslynOptions = roslynOptions.WithSpecificDiagnosticOptions(
|
||||
new Dictionary<string, ReportDiagnostic>
|
||||
{
|
||||
{"CS1701", ReportDiagnostic.Suppress}, // Binding redirects
|
||||
{"CS1702", ReportDiagnostic.Suppress},
|
||||
{"CS1705", ReportDiagnostic.Suppress}
|
||||
});
|
||||
|
||||
if (compilationOptions.AllowUnsafe.HasValue)
|
||||
{
|
||||
roslynOptions = roslynOptions.WithAllowUnsafe(compilationOptions.AllowUnsafe.Value);
|
||||
}
|
||||
|
||||
if (compilationOptions.Optimize.HasValue)
|
||||
{
|
||||
var optimizationLevel = compilationOptions.Optimize.Value ? OptimizationLevel.Debug : OptimizationLevel.Release;
|
||||
roslynOptions = roslynOptions.WithOptimizationLevel(optimizationLevel);
|
||||
}
|
||||
|
||||
if (compilationOptions.WarningsAsErrors.HasValue)
|
||||
{
|
||||
var reportDiagnostic = compilationOptions.WarningsAsErrors.Value ? ReportDiagnostic.Error : ReportDiagnostic.Default;
|
||||
roslynOptions = roslynOptions.WithGeneralDiagnosticOption(reportDiagnostic);
|
||||
}
|
||||
|
||||
options.CompilationOptions = roslynOptions;
|
||||
}
|
||||
|
||||
private static void SetParseOptions(RazorViewEngineOptions options, Microsoft.Extensions.DependencyModel.CompilationOptions compilationOptions)
|
||||
{
|
||||
var roslynParseOptions = options.ParseOptions;
|
||||
roslynParseOptions = roslynParseOptions.WithPreprocessorSymbols(compilationOptions.Defines);
|
||||
|
||||
var languageVersion = roslynParseOptions.LanguageVersion;
|
||||
if (Enum.TryParse(compilationOptions.LanguageVersion, ignoreCase: true, result: out languageVersion))
|
||||
{
|
||||
roslynParseOptions = roslynParseOptions.WithLanguageVersion(languageVersion);
|
||||
}
|
||||
|
||||
options.ParseOptions = roslynParseOptions;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -94,9 +94,22 @@ namespace Microsoft.Extensions.DependencyInjection
|
|||
// Internal for testing.
|
||||
internal static void AddRazorViewEngineServices(IServiceCollection services)
|
||||
{
|
||||
if (CompilationServices.Default != null)
|
||||
var compilationServicesAvailible = CompilationServices.Default != null;
|
||||
|
||||
if (compilationServicesAvailible)
|
||||
{
|
||||
services.TryAddSingleton(CompilationServices.Default.LibraryExporter);
|
||||
|
||||
// This caches compilation related details that are valid across the lifetime of the application.
|
||||
services.TryAddSingleton<ICompilationService, DefaultRoslynCompilationService>();
|
||||
}
|
||||
else
|
||||
{
|
||||
services.TryAddEnumerable(
|
||||
ServiceDescriptor.Transient<IConfigureOptions<RazorViewEngineOptions>, DependencyContextRazorViewEngineOptionsSetup>());
|
||||
|
||||
// This caches compilation related details that are valid across the lifetime of the application.
|
||||
services.TryAddSingleton<ICompilationService, DependencyContextCompilationService>();
|
||||
}
|
||||
|
||||
services.TryAddEnumerable(
|
||||
|
|
@ -117,9 +130,6 @@ namespace Microsoft.Extensions.DependencyInjection
|
|||
// Caches compilation artifacts across the lifetime of the application.
|
||||
services.TryAddSingleton<ICompilerCacheProvider, DefaultCompilerCacheProvider>();
|
||||
|
||||
// This caches compilation related details that are valid across the lifetime of the application.
|
||||
services.TryAddSingleton<ICompilationService, RoslynCompilationService>();
|
||||
|
||||
// In the default scenario the following services are singleton by virtue of being initialized as part of
|
||||
// creating the singleton RazorViewEngine instance.
|
||||
services.TryAddTransient<IRazorPageFactoryProvider, DefaultRazorPageFactoryProvider>();
|
||||
|
|
|
|||
|
|
@ -1,11 +1,11 @@
|
|||
// Copyright (c) .NET Foundation. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.AspNet.Http;
|
||||
using Microsoft.Extensions.PlatformAbstractions;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
|
|
@ -107,14 +107,13 @@ namespace Microsoft.AspNet.Mvc.Infrastructure
|
|||
{
|
||||
// Arrange
|
||||
var provider = new MvcAssembliesTestingProvider();
|
||||
|
||||
var expected = provider.LoadableReferenceAssemblies;
|
||||
var expected = provider.LoadableReferenceAssemblies.OrderBy(p => p, StringComparer.Ordinal);
|
||||
|
||||
// Act
|
||||
var referenceAssemblies = provider.ReferenceAssemblies;
|
||||
var referenceAssemblies = provider.ReferenceAssemblies.OrderBy(p => p, StringComparer.Ordinal);
|
||||
|
||||
// Assert
|
||||
Assert.True(expected.SetEquals(referenceAssemblies));
|
||||
Assert.Equal(expected, referenceAssemblies);
|
||||
}
|
||||
|
||||
private static ILibraryManager CreateLibraryManager()
|
||||
|
|
@ -236,7 +235,6 @@ namespace Microsoft.AspNet.Mvc.Infrastructure
|
|||
// The following assemblies are not reachable from Microsoft.AspNet.Mvc
|
||||
mvcAssemblies.Add("Microsoft.AspNet.Mvc.TagHelpers");
|
||||
mvcAssemblies.Add("Microsoft.AspNet.Mvc.Formatters.Xml");
|
||||
mvcAssemblies.Add("Microsoft.AspNet.PageExecutionInstrumentation.Interfaces");
|
||||
|
||||
return mvcAssemblies;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ using Xunit;
|
|||
|
||||
namespace Microsoft.AspNet.Mvc.Razor.Compilation
|
||||
{
|
||||
public class RoslynCompilationServiceTest
|
||||
public class DefaultRoslynCompilationServiceTest
|
||||
{
|
||||
private const string ConfigurationName = "Release";
|
||||
|
||||
|
|
@ -30,7 +30,7 @@ public class MyTestType {}";
|
|||
mvcRazorHost.SetupGet(m => m.MainClassNamePrefix)
|
||||
.Returns(string.Empty);
|
||||
|
||||
var compilationService = new RoslynCompilationService(
|
||||
var compilationService = new DefaultRoslynCompilationService(
|
||||
applicationEnvironment,
|
||||
libraryExporter,
|
||||
mvcRazorHost.Object,
|
||||
|
|
@ -63,7 +63,7 @@ this should fail";
|
|||
var fileProvider = new TestFileProvider();
|
||||
var fileInfo = fileProvider.AddFile(viewPath, fileContent);
|
||||
|
||||
var compilationService = new RoslynCompilationService(
|
||||
var compilationService = new DefaultRoslynCompilationService(
|
||||
applicationEnvironment,
|
||||
libraryExporter,
|
||||
mvcRazorHost,
|
||||
|
|
@ -93,7 +93,7 @@ this should fail";
|
|||
var libraryExporter = CompilationServices.Default.LibraryExporter;
|
||||
var mvcRazorHost = Mock.Of<IMvcRazorHost>();
|
||||
|
||||
var compilationService = new RoslynCompilationService(
|
||||
var compilationService = new DefaultRoslynCompilationService(
|
||||
applicationEnvironment,
|
||||
libraryExporter,
|
||||
mvcRazorHost,
|
||||
|
|
@ -134,7 +134,7 @@ this should fail";
|
|||
var fileProvider = new TestFileProvider();
|
||||
fileProvider.AddFile(path, mockFileInfo.Object);
|
||||
|
||||
var compilationService = new RoslynCompilationService(
|
||||
var compilationService = new DefaultRoslynCompilationService(
|
||||
applicationEnvironment,
|
||||
libraryExporter,
|
||||
mvcRazorHost,
|
||||
|
|
@ -175,7 +175,7 @@ public class MyNonCustomDefinedClass {}
|
|||
var options = GetOptions();
|
||||
options.Value.ParseOptions = options.Value.ParseOptions.WithPreprocessorSymbols("MY_CUSTOM_DEFINE");
|
||||
|
||||
var compilationService = new RoslynCompilationService(
|
||||
var compilationService = new DefaultRoslynCompilationService(
|
||||
applicationEnvironment,
|
||||
libraryExporter,
|
||||
mvcRazorHost.Object,
|
||||
|
|
@ -207,7 +207,7 @@ public class NotRazorPrefixType {}";
|
|||
mvcRazorHost.SetupGet(m => m.MainClassNamePrefix)
|
||||
.Returns("RazorPrefix");
|
||||
|
||||
var compilationService = new RoslynCompilationService(
|
||||
var compilationService = new DefaultRoslynCompilationService(
|
||||
applicationEnvironment,
|
||||
libraryExporter,
|
||||
mvcRazorHost.Object,
|
||||
|
|
@ -240,7 +240,7 @@ public class NotRazorPrefixType {}";
|
|||
var optionsAccessor = new Mock<IOptions<RazorViewEngineOptions>>();
|
||||
optionsAccessor.SetupGet(o => o.Value)
|
||||
.Returns(options);
|
||||
var compilationService = new RoslynCompilationService(
|
||||
var compilationService = new DefaultRoslynCompilationService(
|
||||
PlatformServices.Default.Application,
|
||||
CompilationServices.Default.LibraryExporter,
|
||||
Mock.Of<IMvcRazorHost>(),
|
||||
|
|
@ -333,7 +333,7 @@ public class NotRazorPrefixType {}";
|
|||
mvcRazorHost.SetupGet(m => m.MainClassNamePrefix)
|
||||
.Returns(string.Empty);
|
||||
|
||||
var compilationService = new RoslynCompilationService(
|
||||
var compilationService = new DefaultRoslynCompilationService(
|
||||
applicationEnvironment,
|
||||
libraryExporter,
|
||||
mvcRazorHost.Object,
|
||||
Loading…
Reference in New Issue