From c3f76137259bea68d4add1aa0d2f79b77a86cfa6 Mon Sep 17 00:00:00 2001 From: Ryan Nowak Date: Thu, 25 May 2017 16:15:31 -0700 Subject: [PATCH] Make RazorReferenceManage public Fix for #4938. This change makes the reference manager public and documented so that it can be replaced. --- .../Compilation/RazorReferenceManager.cs | 19 +++++++++++++++++++ .../MvcRazorMvcCoreBuilderExtensions.cs | 3 +-- .../Internal/CSharpCompiler.cs | 1 + ...ger.cs => DefaultRazorReferenceManager.cs} | 6 +++--- .../Internal/LazyMetadataReferenceFeature.cs | 1 + .../Internal/CSharpCompilerTest.cs | 2 +- ...cs => DefaultRazorReferenceManagerTest.cs} | 6 +++--- .../Internal/RazorViewCompilerProviderTest.cs | 2 +- .../Internal/RazorViewCompilerTest.cs | 4 ++-- 9 files changed, 32 insertions(+), 12 deletions(-) create mode 100644 src/Microsoft.AspNetCore.Mvc.Razor/Compilation/RazorReferenceManager.cs rename src/Microsoft.AspNetCore.Mvc.Razor/Internal/{RazorReferenceManager.cs => DefaultRazorReferenceManager.cs} (91%) rename test/Microsoft.AspNetCore.Mvc.Razor.Test/Internal/{ReferenceManagerTest.cs => DefaultRazorReferenceManagerTest.cs} (91%) diff --git a/src/Microsoft.AspNetCore.Mvc.Razor/Compilation/RazorReferenceManager.cs b/src/Microsoft.AspNetCore.Mvc.Razor/Compilation/RazorReferenceManager.cs new file mode 100644 index 0000000000..5b11ce10e3 --- /dev/null +++ b/src/Microsoft.AspNetCore.Mvc.Razor/Compilation/RazorReferenceManager.cs @@ -0,0 +1,19 @@ +// 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 Microsoft.CodeAnalysis; + +namespace Microsoft.AspNetCore.Mvc.Razor.Compilation +{ + /// + /// Manages compilation references for Razor compilation. + /// + public abstract class RazorReferenceManager + { + /// + /// Gets the set of compilation references to be used for Razor compilation. + /// + public abstract IReadOnlyList CompilationReferences { get; } + } +} diff --git a/src/Microsoft.AspNetCore.Mvc.Razor/DependencyInjection/MvcRazorMvcCoreBuilderExtensions.cs b/src/Microsoft.AspNetCore.Mvc.Razor/DependencyInjection/MvcRazorMvcCoreBuilderExtensions.cs index 7663748620..910434d706 100644 --- a/src/Microsoft.AspNetCore.Mvc.Razor/DependencyInjection/MvcRazorMvcCoreBuilderExtensions.cs +++ b/src/Microsoft.AspNetCore.Mvc.Razor/DependencyInjection/MvcRazorMvcCoreBuilderExtensions.cs @@ -132,8 +132,7 @@ namespace Microsoft.Extensions.DependencyInjection internal static void AddRazorViewEngineServices(IServiceCollection services) { services.TryAddSingleton(); - services.TryAddSingleton(); - // This caches compilation related details that are valid across the lifetime of the application. + services.TryAddSingleton(); services.TryAddEnumerable( ServiceDescriptor.Transient, MvcRazorMvcViewOptionsSetup>()); diff --git a/src/Microsoft.AspNetCore.Mvc.Razor/Internal/CSharpCompiler.cs b/src/Microsoft.AspNetCore.Mvc.Razor/Internal/CSharpCompiler.cs index 1af6274527..ecc179451a 100644 --- a/src/Microsoft.AspNetCore.Mvc.Razor/Internal/CSharpCompiler.cs +++ b/src/Microsoft.AspNetCore.Mvc.Razor/Internal/CSharpCompiler.cs @@ -1,6 +1,7 @@ // 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 Microsoft.AspNetCore.Mvc.Razor.Compilation; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.Emit; diff --git a/src/Microsoft.AspNetCore.Mvc.Razor/Internal/RazorReferenceManager.cs b/src/Microsoft.AspNetCore.Mvc.Razor/Internal/DefaultRazorReferenceManager.cs similarity index 91% rename from src/Microsoft.AspNetCore.Mvc.Razor/Internal/RazorReferenceManager.cs rename to src/Microsoft.AspNetCore.Mvc.Razor/Internal/DefaultRazorReferenceManager.cs index 4d5338ae5e..f1d59ef770 100644 --- a/src/Microsoft.AspNetCore.Mvc.Razor/Internal/RazorReferenceManager.cs +++ b/src/Microsoft.AspNetCore.Mvc.Razor/Internal/DefaultRazorReferenceManager.cs @@ -11,7 +11,7 @@ using Microsoft.Extensions.Options; namespace Microsoft.AspNetCore.Mvc.Razor.Internal { - public class RazorReferenceManager + public class DefaultRazorReferenceManager : RazorReferenceManager { private readonly ApplicationPartManager _partManager; private readonly IList _additionalMetadataReferences; @@ -19,7 +19,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal private bool _compilationReferencesInitialized; private IReadOnlyList _compilationReferences; - public RazorReferenceManager( + public DefaultRazorReferenceManager( ApplicationPartManager partManager, IOptions optionsAccessor) { @@ -27,7 +27,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal _additionalMetadataReferences = optionsAccessor.Value.AdditionalCompilationReferences; } - public IReadOnlyList CompilationReferences + public override IReadOnlyList CompilationReferences { get { diff --git a/src/Microsoft.AspNetCore.Mvc.Razor/Internal/LazyMetadataReferenceFeature.cs b/src/Microsoft.AspNetCore.Mvc.Razor/Internal/LazyMetadataReferenceFeature.cs index c9c040a812..ead4b29630 100644 --- a/src/Microsoft.AspNetCore.Mvc.Razor/Internal/LazyMetadataReferenceFeature.cs +++ b/src/Microsoft.AspNetCore.Mvc.Razor/Internal/LazyMetadataReferenceFeature.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using Microsoft.AspNetCore.Razor.Language; +using Microsoft.AspNetCore.Mvc.Razor.Compilation; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.Razor; diff --git a/test/Microsoft.AspNetCore.Mvc.Razor.Test/Internal/CSharpCompilerTest.cs b/test/Microsoft.AspNetCore.Mvc.Razor.Test/Internal/CSharpCompilerTest.cs index a33723941c..9bb71e41c9 100644 --- a/test/Microsoft.AspNetCore.Mvc.Razor.Test/Internal/CSharpCompilerTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Razor.Test/Internal/CSharpCompilerTest.cs @@ -20,7 +20,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal var define = "MY_CUSTOM_DEFINE"; var options = new TestOptionsManager(); options.Value.ParseOptions = options.Value.ParseOptions.WithPreprocessorSymbols(define); - var razorReferenceManager = new RazorReferenceManager(GetApplicationPartManager(), options); + var razorReferenceManager = new DefaultRazorReferenceManager(GetApplicationPartManager(), options); var compiler = new CSharpCompiler(razorReferenceManager, options); // Act diff --git a/test/Microsoft.AspNetCore.Mvc.Razor.Test/Internal/ReferenceManagerTest.cs b/test/Microsoft.AspNetCore.Mvc.Razor.Test/Internal/DefaultRazorReferenceManagerTest.cs similarity index 91% rename from test/Microsoft.AspNetCore.Mvc.Razor.Test/Internal/ReferenceManagerTest.cs rename to test/Microsoft.AspNetCore.Mvc.Razor.Test/Internal/DefaultRazorReferenceManagerTest.cs index 30a3f9893c..2e970db018 100644 --- a/test/Microsoft.AspNetCore.Mvc.Razor.Test/Internal/ReferenceManagerTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Razor.Test/Internal/DefaultRazorReferenceManagerTest.cs @@ -11,7 +11,7 @@ using Xunit; namespace Microsoft.AspNetCore.Mvc.Razor.Test.Internal { - public class ReferenceManagerTest + public class DefaultRazorReferenceManagerTest { [Fact] public void GetCompilationReferences_CombinesApplicationPartAndOptionMetadataReferences() @@ -29,7 +29,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Test.Internal var expectedReferenceDisplays = partReferences .Concat(new[] { objectAssemblyMetadataReference }) .Select(r => r.Display); - var referenceManager = new RazorReferenceManager( + var referenceManager = new DefaultRazorReferenceManager( applicationPartManager, new TestOptionsManager(options)); @@ -44,7 +44,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Test.Internal private static ApplicationPartManager GetApplicationPartManager() { var applicationPartManager = new ApplicationPartManager(); - var assembly = typeof(ReferenceManagerTest).GetTypeInfo().Assembly; + var assembly = typeof(DefaultRazorReferenceManagerTest).GetTypeInfo().Assembly; applicationPartManager.ApplicationParts.Add(new AssemblyPart(assembly)); applicationPartManager.FeatureProviders.Add(new MetadataReferenceFeatureProvider()); diff --git a/test/Microsoft.AspNetCore.Mvc.Razor.Test/Internal/RazorViewCompilerProviderTest.cs b/test/Microsoft.AspNetCore.Mvc.Razor.Test/Internal/RazorViewCompilerProviderTest.cs index c1007474f0..b05d4b16df 100644 --- a/test/Microsoft.AspNetCore.Mvc.Razor.Test/Internal/RazorViewCompilerProviderTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Razor.Test/Internal/RazorViewCompilerProviderTest.cs @@ -25,7 +25,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal var accessor = new Mock(); var applicationManager = new ApplicationPartManager(); var options = new TestOptionsManager(); - var referenceManager = new RazorReferenceManager(applicationManager, options); + var referenceManager = new DefaultRazorReferenceManager(applicationManager, options); accessor.Setup(a => a.FileProvider).Returns(fileProvider); var provider = new RazorViewCompilerProvider( applicationManager, diff --git a/test/Microsoft.AspNetCore.Mvc.Razor.Test/Internal/RazorViewCompilerTest.cs b/test/Microsoft.AspNetCore.Mvc.Razor.Test/Internal/RazorViewCompilerTest.cs index 17d3c8f139..62550fd260 100644 --- a/test/Microsoft.AspNetCore.Mvc.Razor.Test/Internal/RazorViewCompilerTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Razor.Test/Internal/RazorViewCompilerTest.cs @@ -440,7 +440,7 @@ this should fail"; }; var applicationPartManager = new ApplicationPartManager(); - var referenceManager = new RazorReferenceManager( + var referenceManager = new DefaultRazorReferenceManager( applicationPartManager, new TestOptionsManager()); var compiler = GetViewCompiler( @@ -472,7 +472,7 @@ this should fail"; applicationPartManager.ApplicationParts.Add(new AssemblyPart(assembly)); applicationPartManager.FeatureProviders.Add(new MetadataReferenceFeatureProvider()); - referenceManager = new RazorReferenceManager(applicationPartManager, options); + referenceManager = new DefaultRazorReferenceManager(applicationPartManager, options); } precompiledViews = precompiledViews ?? Array.Empty();