Revert DependencyContext changes untill dotnet-cli packages get sorted out
This commit is contained in:
parent
b5c48de651
commit
57bf12311b
|
|
@ -1,7 +1,6 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<configuration>
|
||||
<packageSources>
|
||||
<add key="dotnet-cli" value="https://www.myget.org/F/dotnet-cli/api/v3/index.json" />
|
||||
<add key="AspNetVNext" value="https://www.myget.org/F/aspnetcidev/api/v3/index.json" />
|
||||
<add key="NuGet" value="https://api.nuget.org/v3/index.json" />
|
||||
</packageSources>
|
||||
|
|
|
|||
|
|
@ -78,15 +78,7 @@ namespace Microsoft.Extensions.DependencyInjection
|
|||
// Action Discovery
|
||||
//
|
||||
// These are consumed only when creating action descriptors, then they can be de-allocated
|
||||
if (PlatformServices.Default?.LibraryManager != null)
|
||||
{
|
||||
services.TryAddTransient<IAssemblyProvider, DefaultAssemblyProvider>();
|
||||
}
|
||||
else
|
||||
{
|
||||
services.TryAddTransient<IAssemblyProvider, DependencyContextAssemblyProvider>();
|
||||
}
|
||||
|
||||
services.TryAddTransient<IAssemblyProvider, DefaultAssemblyProvider>();
|
||||
services.TryAddTransient<IControllerTypeProvider, DefaultControllerTypeProvider>();
|
||||
services.TryAddEnumerable(
|
||||
ServiceDescriptor.Transient<IApplicationModelProvider, DefaultApplicationModelProvider>());
|
||||
|
|
|
|||
|
|
@ -41,7 +41,8 @@ namespace Microsoft.AspNet.Mvc.Infrastructure
|
|||
"Microsoft.AspNet.Mvc.Razor",
|
||||
"Microsoft.AspNet.Mvc.Razor.Host",
|
||||
"Microsoft.AspNet.Mvc.TagHelpers",
|
||||
"Microsoft.AspNet.Mvc.ViewFeatures"
|
||||
"Microsoft.AspNet.Mvc.ViewFeatures",
|
||||
"Microsoft.AspNet.PageExecutionInstrumentation.Interfaces",
|
||||
};
|
||||
|
||||
/// <inheritdoc />
|
||||
|
|
|
|||
|
|
@ -1,78 +0,0 @@
|
|||
// 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,7 +21,6 @@
|
|||
"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"
|
||||
|
|
@ -45,7 +44,6 @@
|
|||
"frameworks": {
|
||||
"net451": {
|
||||
"frameworkAssemblies": {
|
||||
"System.Reflection": "",
|
||||
"System.Runtime": ""
|
||||
}
|
||||
},
|
||||
|
|
|
|||
|
|
@ -1,118 +0,0 @@
|
|||
// 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.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>
|
||||
public DefaultRoslynCompilationService(IApplicationEnvironment environment,
|
||||
ILibraryExporter libraryExporter,
|
||||
IMvcRazorHost host,
|
||||
IOptions<RazorViewEngineOptions> optionsAccessor,
|
||||
IRazorViewEngineFileProviderAccessor fileProviderAccessor)
|
||||
: base(environment, host, optionsAccessor, fileProviderAccessor)
|
||||
{
|
||||
_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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,62 +0,0 @@
|
|||
// 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.CompilationAbstractions;
|
||||
using Microsoft.Extensions.DependencyModel;
|
||||
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 find out references.
|
||||
/// </summary>
|
||||
public class DependencyContextCompilationService : RoslynCompilationService
|
||||
{
|
||||
private 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>
|
||||
public DependencyContextCompilationService(IApplicationEnvironment environment,
|
||||
IMvcRazorHost host,
|
||||
IOptions<RazorViewEngineOptions> optionsAccessor,
|
||||
IRazorViewEngineFileProviderAccessor fileProviderAccessor)
|
||||
: this(DependencyContext.Default, environment, host, optionsAccessor, fileProviderAccessor)
|
||||
{
|
||||
}
|
||||
|
||||
/// <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>
|
||||
public DependencyContextCompilationService(DependencyContext dependencyContext,
|
||||
IApplicationEnvironment environment,
|
||||
IMvcRazorHost host,
|
||||
IOptions<RazorViewEngineOptions> optionsAccessor,
|
||||
IRazorViewEngineFileProviderAccessor fileProviderAccessor)
|
||||
: base(environment, host, optionsAccessor, fileProviderAccessor)
|
||||
{
|
||||
_dependencyContext = dependencyContext;
|
||||
}
|
||||
|
||||
protected override List<MetadataReference> GetApplicationReferences()
|
||||
{
|
||||
return _dependencyContext.CompileLibraries
|
||||
.SelectMany(library => library.ResolveReferencePaths())
|
||||
.Select(CreateMetadataFileReference)
|
||||
.ToList();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -4,6 +4,7 @@
|
|||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
|
@ -12,7 +13,6 @@ 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,6 +22,7 @@ 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
|
||||
|
|
@ -29,12 +30,13 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation
|
|||
/// <summary>
|
||||
/// A type that uses Roslyn to compile C# content.
|
||||
/// </summary>
|
||||
public abstract class RoslynCompilationService : ICompilationService
|
||||
public 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;
|
||||
|
|
@ -52,17 +54,20 @@ 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;
|
||||
|
|
@ -234,9 +239,83 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation
|
|||
return diagnostic.Location.GetMappedLineSpan().Path;
|
||||
}
|
||||
|
||||
protected abstract List<MetadataReference> GetApplicationReferences();
|
||||
private List<MetadataReference> GetApplicationReferences()
|
||||
{
|
||||
var references = new List<MetadataReference>();
|
||||
|
||||
protected MetadataReference CreateMetadataFileReference(string path)
|
||||
// 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)
|
||||
{
|
||||
var metadata = _metadataFileCache.GetOrAdd(path, _ =>
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,88 +0,0 @@
|
|||
// 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -119,22 +119,9 @@ namespace Microsoft.Extensions.DependencyInjection
|
|||
// Internal for testing.
|
||||
internal static void AddRazorViewEngineServices(IServiceCollection services)
|
||||
{
|
||||
var compilationServicesAvailible = CompilationServices.Default != null;
|
||||
|
||||
if (compilationServicesAvailible)
|
||||
if (CompilationServices.Default != null)
|
||||
{
|
||||
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(
|
||||
|
|
@ -155,6 +142,9 @@ 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>();
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ using Xunit;
|
|||
|
||||
namespace Microsoft.AspNet.Mvc.Razor.Compilation
|
||||
{
|
||||
public class DefaultRoslynCompilationServiceTest
|
||||
public class RoslynCompilationServiceTest
|
||||
{
|
||||
private const string ConfigurationName = "Release";
|
||||
|
||||
|
|
@ -30,7 +30,7 @@ public class MyTestType {}";
|
|||
mvcRazorHost.SetupGet(m => m.MainClassNamePrefix)
|
||||
.Returns(string.Empty);
|
||||
|
||||
var compilationService = new DefaultRoslynCompilationService(
|
||||
var compilationService = new RoslynCompilationService(
|
||||
applicationEnvironment,
|
||||
libraryExporter,
|
||||
mvcRazorHost.Object,
|
||||
|
|
@ -63,7 +63,7 @@ this should fail";
|
|||
var fileProvider = new TestFileProvider();
|
||||
var fileInfo = fileProvider.AddFile(viewPath, fileContent);
|
||||
|
||||
var compilationService = new DefaultRoslynCompilationService(
|
||||
var compilationService = new RoslynCompilationService(
|
||||
applicationEnvironment,
|
||||
libraryExporter,
|
||||
mvcRazorHost,
|
||||
|
|
@ -93,7 +93,7 @@ this should fail";
|
|||
var libraryExporter = CompilationServices.Default.LibraryExporter;
|
||||
var mvcRazorHost = Mock.Of<IMvcRazorHost>();
|
||||
|
||||
var compilationService = new DefaultRoslynCompilationService(
|
||||
var compilationService = new RoslynCompilationService(
|
||||
applicationEnvironment,
|
||||
libraryExporter,
|
||||
mvcRazorHost,
|
||||
|
|
@ -134,7 +134,7 @@ this should fail";
|
|||
var fileProvider = new TestFileProvider();
|
||||
fileProvider.AddFile(path, mockFileInfo.Object);
|
||||
|
||||
var compilationService = new DefaultRoslynCompilationService(
|
||||
var compilationService = new RoslynCompilationService(
|
||||
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 DefaultRoslynCompilationService(
|
||||
var compilationService = new RoslynCompilationService(
|
||||
applicationEnvironment,
|
||||
libraryExporter,
|
||||
mvcRazorHost.Object,
|
||||
|
|
@ -207,7 +207,7 @@ public class NotRazorPrefixType {}";
|
|||
mvcRazorHost.SetupGet(m => m.MainClassNamePrefix)
|
||||
.Returns("RazorPrefix");
|
||||
|
||||
var compilationService = new DefaultRoslynCompilationService(
|
||||
var compilationService = new RoslynCompilationService(
|
||||
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 DefaultRoslynCompilationService(
|
||||
var compilationService = new RoslynCompilationService(
|
||||
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 DefaultRoslynCompilationService(
|
||||
var compilationService = new RoslynCompilationService(
|
||||
applicationEnvironment,
|
||||
libraryExporter,
|
||||
mvcRazorHost.Object,
|
||||
Loading…
Reference in New Issue