From 57bf12311b592ee2b450c03534a3eda68c4e19dc Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Fri, 8 Jan 2016 15:59:53 -0800 Subject: [PATCH] Revert DependencyContext changes untill dotnet-cli packages get sorted out --- NuGet.config | 1 - .../MvcCoreServiceCollectionExtensions.cs | 10 +- .../Infrastructure/DefaultAssemblyProvider.cs | 3 +- .../DependencyContextAssemblyProvider.cs | 78 ------------ src/Microsoft.AspNet.Mvc.Core/project.json | 2 - .../DefaultRoslynCompilationService.cs | 118 ------------------ .../DependencyContextCompilationService.cs | 62 --------- .../Compilation/RoslynCompilationService.cs | 87 ++++++++++++- ...dencyContextRazorViewEngineOptionsSetup.cs | 88 ------------- .../MvcRazorMvcCoreBuilderExtensions.cs | 18 +-- ...est.cs => RoslynCompilationServiceTest.cs} | 18 +-- 11 files changed, 99 insertions(+), 386 deletions(-) delete mode 100644 src/Microsoft.AspNet.Mvc.Core/Infrastructure/DependencyContextAssemblyProvider.cs delete mode 100644 src/Microsoft.AspNet.Mvc.Razor/Compilation/DefaultRoslynCompilationService.cs delete mode 100644 src/Microsoft.AspNet.Mvc.Razor/Compilation/DependencyContextCompilationService.cs delete mode 100644 src/Microsoft.AspNet.Mvc.Razor/DependencyContextRazorViewEngineOptionsSetup.cs rename test/Microsoft.AspNet.Mvc.Razor.Test/Compilation/{DefaultRoslynCompilationServiceTest.cs => RoslynCompilationServiceTest.cs} (95%) diff --git a/NuGet.config b/NuGet.config index 02c66ca7d0..5500f6d507 100644 --- a/NuGet.config +++ b/NuGet.config @@ -1,7 +1,6 @@  - diff --git a/src/Microsoft.AspNet.Mvc.Core/DependencyInjection/MvcCoreServiceCollectionExtensions.cs b/src/Microsoft.AspNet.Mvc.Core/DependencyInjection/MvcCoreServiceCollectionExtensions.cs index a7309fcd16..110f87b70c 100644 --- a/src/Microsoft.AspNet.Mvc.Core/DependencyInjection/MvcCoreServiceCollectionExtensions.cs +++ b/src/Microsoft.AspNet.Mvc.Core/DependencyInjection/MvcCoreServiceCollectionExtensions.cs @@ -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(); - } - else - { - services.TryAddTransient(); - } - + services.TryAddTransient(); services.TryAddTransient(); services.TryAddEnumerable( ServiceDescriptor.Transient()); diff --git a/src/Microsoft.AspNet.Mvc.Core/Infrastructure/DefaultAssemblyProvider.cs b/src/Microsoft.AspNet.Mvc.Core/Infrastructure/DefaultAssemblyProvider.cs index ffdf3d39ae..788fb0a949 100644 --- a/src/Microsoft.AspNet.Mvc.Core/Infrastructure/DefaultAssemblyProvider.cs +++ b/src/Microsoft.AspNet.Mvc.Core/Infrastructure/DefaultAssemblyProvider.cs @@ -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", }; /// diff --git a/src/Microsoft.AspNet.Mvc.Core/Infrastructure/DependencyContextAssemblyProvider.cs b/src/Microsoft.AspNet.Mvc.Core/Infrastructure/DependencyContextAssemblyProvider.cs deleted file mode 100644 index 235769398c..0000000000 --- a/src/Microsoft.AspNet.Mvc.Core/Infrastructure/DependencyContextAssemblyProvider.cs +++ /dev/null @@ -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 - { - /// - /// Gets the set of assembly names that are used as root for discovery of - /// MVC controllers, view components and views. - /// - // 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 ReferenceAssemblies { get; } = new HashSet(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" - }; - - /// - public IEnumerable CandidateAssemblies - { - get - { - return GetCandidateLibraries() - .SelectMany(l => l.Assemblies) - .Select(Load); - } - } - - /// - /// Returns a list of libraries that references the assemblies in . - /// By default it returns all assemblies that reference any of the primary MVC assemblies - /// while ignoring MVC assemblies. - /// - /// A set of . - protected virtual IEnumerable GetCandidateLibraries() - { - if (ReferenceAssemblies == null) - { - return Enumerable.Empty(); - } - - 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)); - } - } -} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Core/project.json b/src/Microsoft.AspNet.Mvc.Core/project.json index abff865d15..e16c385e12 100644 --- a/src/Microsoft.AspNet.Mvc.Core/project.json +++ b/src/Microsoft.AspNet.Mvc.Core/project.json @@ -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": "" } }, diff --git a/src/Microsoft.AspNet.Mvc.Razor/Compilation/DefaultRoslynCompilationService.cs b/src/Microsoft.AspNet.Mvc.Razor/Compilation/DefaultRoslynCompilationService.cs deleted file mode 100644 index 8d1b5fca76..0000000000 --- a/src/Microsoft.AspNet.Mvc.Razor/Compilation/DefaultRoslynCompilationService.cs +++ /dev/null @@ -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 -{ - /// - /// A type that uses Roslyn to compile C# content and to find out references. - /// - public class DefaultRoslynCompilationService : RoslynCompilationService - { - private readonly IApplicationEnvironment _environment; - private readonly ILibraryExporter _libraryExporter; - - /// - /// Initalizes a new instance of the class. - /// - /// The environment for the executing application. - /// The library manager that provides export and reference information. - /// The that was used to generate the code. - /// Accessor to . - /// The . - public DefaultRoslynCompilationService(IApplicationEnvironment environment, - ILibraryExporter libraryExporter, - IMvcRazorHost host, - IOptions optionsAccessor, - IRazorViewEngineFileProviderAccessor fileProviderAccessor) - : base(environment, host, optionsAccessor, fileProviderAccessor) - { - _environment = environment; - _libraryExporter = libraryExporter; - } - - protected override List GetApplicationReferences() - { - var references = new List(); - - // 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(); - } - } -} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Razor/Compilation/DependencyContextCompilationService.cs b/src/Microsoft.AspNet.Mvc.Razor/Compilation/DependencyContextCompilationService.cs deleted file mode 100644 index 7522fe0852..0000000000 --- a/src/Microsoft.AspNet.Mvc.Razor/Compilation/DependencyContextCompilationService.cs +++ /dev/null @@ -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 -{ - /// - /// A type that uses Roslyn to compile C# content and to find out references. - /// - public class DependencyContextCompilationService : RoslynCompilationService - { - private DependencyContext _dependencyContext; - - /// - /// Initalizes a new instance of the class. - /// - /// The environment for the executing application. - /// The that was used to generate the code. - /// Accessor to . - /// The . - public DependencyContextCompilationService(IApplicationEnvironment environment, - IMvcRazorHost host, - IOptions optionsAccessor, - IRazorViewEngineFileProviderAccessor fileProviderAccessor) - : this(DependencyContext.Default, environment, host, optionsAccessor, fileProviderAccessor) - { - } - - /// - /// Initalizes a new instance of the class. - /// - /// to use for reference resolution. - /// The environment for the executing application. - /// The that was used to generate the code. - /// Accessor to . - /// The . - public DependencyContextCompilationService(DependencyContext dependencyContext, - IApplicationEnvironment environment, - IMvcRazorHost host, - IOptions optionsAccessor, - IRazorViewEngineFileProviderAccessor fileProviderAccessor) - : base(environment, host, optionsAccessor, fileProviderAccessor) - { - _dependencyContext = dependencyContext; - } - - protected override List GetApplicationReferences() - { - return _dependencyContext.CompileLibraries - .SelectMany(library => library.ResolveReferencePaths()) - .Select(CreateMetadataFileReference) - .ToList(); - } - } -} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Razor/Compilation/RoslynCompilationService.cs b/src/Microsoft.AspNet.Mvc.Razor/Compilation/RoslynCompilationService.cs index e3f91db0db..0e6f74041e 100644 --- a/src/Microsoft.AspNet.Mvc.Razor/Compilation/RoslynCompilationService.cs +++ b/src/Microsoft.AspNet.Mvc.Razor/Compilation/RoslynCompilationService.cs @@ -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 /// /// A type that uses Roslyn to compile C# content. /// - public abstract class RoslynCompilationService : ICompilationService + public class RoslynCompilationService : ICompilationService { private readonly Lazy _supportsPdbGeneration = new Lazy(SymbolsUtility.SupportsSymbolsGeneration); private readonly ConcurrentDictionary _metadataFileCache = new ConcurrentDictionary(StringComparer.OrdinalIgnoreCase); + private readonly Extensions.CompilationAbstractions.ILibraryExporter _libraryExporter; private readonly IApplicationEnvironment _environment; private readonly IFileProvider _fileProvider; private readonly Lazy> _applicationReferences; @@ -52,17 +54,20 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation /// Initalizes a new instance of the class. /// /// The environment for the executing application. + /// The library manager that provides export and reference information. /// The that was used to generate the code. /// Accessor to . /// The . public RoslynCompilationService( IApplicationEnvironment environment, + Extensions.CompilationAbstractions.ILibraryExporter libraryExporter, IMvcRazorHost host, IOptions optionsAccessor, IRazorViewEngineFileProviderAccessor fileProviderAccessor, ILoggerFactory loggerFactory) { _environment = environment; + _libraryExporter = libraryExporter; _applicationReferences = new Lazy>(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 GetApplicationReferences(); + private List GetApplicationReferences() + { + var references = new List(); - 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, _ => { diff --git a/src/Microsoft.AspNet.Mvc.Razor/DependencyContextRazorViewEngineOptionsSetup.cs b/src/Microsoft.AspNet.Mvc.Razor/DependencyContextRazorViewEngineOptionsSetup.cs deleted file mode 100644 index d87b34d817..0000000000 --- a/src/Microsoft.AspNet.Mvc.Razor/DependencyContextRazorViewEngineOptionsSetup.cs +++ /dev/null @@ -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 -{ - /// - /// Sets up compilation and parse option default options for using - /// - public class DependencyContextRazorViewEngineOptionsSetup : ConfigureOptions - { - /// - /// Initializes a new instance of . - /// - public DependencyContextRazorViewEngineOptionsSetup() : this(DependencyContext.Default) - { - } - - /// - /// Initializes a new instance of . - /// - /// to use as compilation and parse option source. - 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 - { - {"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; - } - } -} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Razor/DependencyInjection/MvcRazorMvcCoreBuilderExtensions.cs b/src/Microsoft.AspNet.Mvc.Razor/DependencyInjection/MvcRazorMvcCoreBuilderExtensions.cs index 37f707154a..fbd46c3d09 100644 --- a/src/Microsoft.AspNet.Mvc.Razor/DependencyInjection/MvcRazorMvcCoreBuilderExtensions.cs +++ b/src/Microsoft.AspNet.Mvc.Razor/DependencyInjection/MvcRazorMvcCoreBuilderExtensions.cs @@ -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(); - } - else - { - services.TryAddEnumerable( - ServiceDescriptor.Transient, DependencyContextRazorViewEngineOptionsSetup>()); - - // This caches compilation related details that are valid across the lifetime of the application. - services.TryAddSingleton(); } services.TryAddEnumerable( @@ -155,6 +142,9 @@ namespace Microsoft.Extensions.DependencyInjection // Caches compilation artifacts across the lifetime of the application. services.TryAddSingleton(); + // This caches compilation related details that are valid across the lifetime of the application. + services.TryAddSingleton(); + // In the default scenario the following services are singleton by virtue of being initialized as part of // creating the singleton RazorViewEngine instance. services.TryAddTransient(); diff --git a/test/Microsoft.AspNet.Mvc.Razor.Test/Compilation/DefaultRoslynCompilationServiceTest.cs b/test/Microsoft.AspNet.Mvc.Razor.Test/Compilation/RoslynCompilationServiceTest.cs similarity index 95% rename from test/Microsoft.AspNet.Mvc.Razor.Test/Compilation/DefaultRoslynCompilationServiceTest.cs rename to test/Microsoft.AspNet.Mvc.Razor.Test/Compilation/RoslynCompilationServiceTest.cs index 73c2eb4834..dc870f2de4 100644 --- a/test/Microsoft.AspNet.Mvc.Razor.Test/Compilation/DefaultRoslynCompilationServiceTest.cs +++ b/test/Microsoft.AspNet.Mvc.Razor.Test/Compilation/RoslynCompilationServiceTest.cs @@ -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(); - 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>(); optionsAccessor.SetupGet(o => o.Value) .Returns(options); - var compilationService = new DefaultRoslynCompilationService( + var compilationService = new RoslynCompilationService( PlatformServices.Default.Application, CompilationServices.Default.LibraryExporter, Mock.Of(), @@ -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,