Add LazyReferenceMetadataFeature

This commit is contained in:
Pranav K 2017-04-19 16:09:03 -07:00
parent 681b798a2e
commit 9fe480f735
3 changed files with 36 additions and 10 deletions

View File

@ -155,13 +155,13 @@ namespace Microsoft.Extensions.DependencyInjection
// creating the singleton RazorViewEngine instance. // creating the singleton RazorViewEngine instance.
services.TryAddTransient<IRazorPageFactoryProvider, DefaultRazorPageFactoryProvider>(); services.TryAddTransient<IRazorPageFactoryProvider, DefaultRazorPageFactoryProvider>();
// //
// Razor compilation infrastructure // Razor compilation infrastructure
// //
services.TryAddSingleton<RazorProject, DefaultRazorProject>(); services.TryAddSingleton<RazorProject, DefaultRazorProject>();
services.TryAddSingleton<RazorTemplateEngine, MvcRazorTemplateEngine>(); services.TryAddSingleton<RazorTemplateEngine, MvcRazorTemplateEngine>();
services.TryAddSingleton<RazorCompiler>(); services.TryAddSingleton<RazorCompiler>();
services.TryAddSingleton<LazyMetadataReferenceFeature>();
services.TryAddSingleton<RazorEngine>(s => services.TryAddSingleton<RazorEngine>(s =>
{ {
@ -171,11 +171,8 @@ namespace Microsoft.Extensions.DependencyInjection
b.Features.Add(new Microsoft.CodeAnalysis.Razor.DefaultTagHelperFeature()); b.Features.Add(new Microsoft.CodeAnalysis.Razor.DefaultTagHelperFeature());
var referenceManager = s.GetRequiredService<RazorReferenceManager>(); var metadataReferenceFeature = s.GetRequiredService<LazyMetadataReferenceFeature>();
b.Features.Add(new Microsoft.CodeAnalysis.Razor.DefaultMetadataReferenceFeature() b.Features.Add(metadataReferenceFeature);
{
References = referenceManager.CompilationReferences.ToArray(),
});
}); });
}); });

View File

@ -0,0 +1,28 @@
// 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.AspNetCore.Razor.Language;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Razor;
namespace Microsoft.AspNetCore.Mvc.Razor.Internal
{
public class LazyMetadataReferenceFeature : IMetadataReferenceFeature
{
private readonly RazorReferenceManager _referenceManager;
public LazyMetadataReferenceFeature(RazorReferenceManager referenceManager)
{
_referenceManager = referenceManager;
}
/// <remarks>
/// Invoking <see cref="RazorReferenceManager.CompilationReferences"/> ensures that compilation
/// references are lazily evaluated.
/// </remarks>
public IReadOnlyList<MetadataReference> References => _referenceManager.CompilationReferences;
public RazorEngine Engine { get; set; }
}
}

View File

@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Threading; using System.Threading;
using Microsoft.AspNetCore.Mvc.ApplicationParts; using Microsoft.AspNetCore.Mvc.ApplicationParts;
using Microsoft.AspNetCore.Mvc.Razor.Compilation; using Microsoft.AspNetCore.Mvc.Razor.Compilation;
@ -16,7 +17,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal
private readonly IList<MetadataReference> _additionalMetadataReferences; private readonly IList<MetadataReference> _additionalMetadataReferences;
private object _compilationReferencesLock = new object(); private object _compilationReferencesLock = new object();
private bool _compilationReferencesInitialized; private bool _compilationReferencesInitialized;
private IList<MetadataReference> _compilationReferences; private IReadOnlyList<MetadataReference> _compilationReferences;
public RazorReferenceManager( public RazorReferenceManager(
ApplicationPartManager partManager, ApplicationPartManager partManager,
@ -26,7 +27,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal
_additionalMetadataReferences = optionsAccessor.Value.AdditionalCompilationReferences; _additionalMetadataReferences = optionsAccessor.Value.AdditionalCompilationReferences;
} }
public IList<MetadataReference> CompilationReferences public IReadOnlyList<MetadataReference> CompilationReferences
{ {
get get
{ {
@ -38,7 +39,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal
} }
} }
private IList<MetadataReference> GetCompilationReferences() private IReadOnlyList<MetadataReference> GetCompilationReferences()
{ {
var feature = new MetadataReferenceFeature(); var feature = new MetadataReferenceFeature();
_partManager.PopulateFeature(feature); _partManager.PopulateFeature(feature);
@ -46,7 +47,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal
if (_additionalMetadataReferences.Count == 0) if (_additionalMetadataReferences.Count == 0)
{ {
return applicationReferences; return applicationReferences.ToArray();
} }
var compilationReferences = new List<MetadataReference>(applicationReferences.Count + _additionalMetadataReferences.Count); var compilationReferences = new List<MetadataReference>(applicationReferences.Count + _additionalMetadataReferences.Count);