Ensure view caches are cleared when RazorHotReload.ClearCache is invoked (#37854) (#37856)

This commit is contained in:
Pranav K 2021-10-26 16:05:44 -07:00 коммит произвёл GitHub
Родитель f3b0c98117
Коммит ecbf0fbc50
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
7 изменённых файлов: 96 добавлений и 8 удалений

Просмотреть файл

@ -74,6 +74,8 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Compilation
_compiledViews = compiledViews;
}
internal Dictionary<string, Task<CompiledViewDescriptor>>? CompiledViews => _compiledViews;
// Invoked as part of a hot reload event.
internal void ClearCache()
{

Просмотреть файл

@ -18,6 +18,8 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Compilation
_compiler = new DefaultViewCompiler(applicationPartManager, loggerFactory.CreateLogger<DefaultViewCompiler>());
}
internal DefaultViewCompiler Compiler => _compiler;
public IViewCompiler GetCompiler() => _compiler;
}
}

Просмотреть файл

@ -28,9 +28,9 @@ namespace Microsoft.AspNetCore.Mvc.Razor
// For Razor view services, use the service locator pattern because they views not be registered by default.
_razorCompiledItemFeatureProvider = applicationPartManager.FeatureProviders.OfType<RazorCompiledItemFeatureProvider>().FirstOrDefault();
if (viewCompilerProvider is DefaultViewCompiler defaultViewCompiler)
if (viewCompilerProvider is DefaultViewCompilerProvider defaultViewCompilerProvider)
{
_defaultViewCompiler = defaultViewCompiler;
_defaultViewCompiler = defaultViewCompilerProvider.Compiler;
}
if (razorViewEngine.GetType() == typeof(RazorViewEngine))

Просмотреть файл

@ -53,6 +53,8 @@ namespace Microsoft.AspNetCore.Mvc.Razor
_activationInfo.Clear();
}
internal ConcurrentDictionary<CacheKey, RazorPagePropertyActivator> ActivationInfo => _activationInfo;
/// <inheritdoc />
public void Activate(IRazorPage page, ViewContext context)
{
@ -107,7 +109,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor
return propertyActivator;
}
private readonly struct CacheKey : IEquatable<CacheKey>
internal readonly struct CacheKey : IEquatable<CacheKey>
{
public CacheKey(Type pageType, Type? providedModelType)
{

Просмотреть файл

@ -89,7 +89,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor
/// <summary>
/// A cache for results of view lookups.
/// </summary>
protected IMemoryCache ViewLookupCache { get; private set; }
protected internal IMemoryCache ViewLookupCache { get; private set; }
/// <summary>
/// Gets the case-normalized route value for the specified route <paramref name="key"/>.

Просмотреть файл

@ -1,12 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.ApplicationParts;
using Microsoft.Extensions.Logging.Abstractions;
using Xunit;
namespace Microsoft.AspNetCore.Mvc.Razor.Compilation
{

Просмотреть файл

@ -0,0 +1,86 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.Razor.Compilation;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
namespace Microsoft.AspNetCore.Mvc.Razor;
public class RazorHotReloadTest
{
[Fact]
public void ClearCache_CanClearViewCompiler()
{
// Regression test for https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1425693
// Arrange
var serviceProvider = GetServiceProvider();
var compilerProvider = Assert.IsType<DefaultViewCompilerProvider>(serviceProvider.GetRequiredService<IViewCompilerProvider>());
var hotReload = serviceProvider.GetRequiredService<RazorHotReload>();
// Act
hotReload.ClearCache(Type.EmptyTypes);
// Assert
Assert.Null(compilerProvider.Compiler.CompiledViews);
}
[Fact]
public void ClearCache_ResetsViewEngineLookupCache()
{
// Arrange
var serviceProvider = GetServiceProvider();
var viewEngine = Assert.IsType<RazorViewEngine>(serviceProvider.GetRequiredService<IRazorViewEngine>());
var hotReload = serviceProvider.GetRequiredService<RazorHotReload>();
var lookup = viewEngine.ViewLookupCache;
// Act
hotReload.ClearCache(Type.EmptyTypes);
// Assert
Assert.NotSame(lookup, viewEngine.ViewLookupCache);
}
[Fact]
public void ClearCache_ResetsRazorPageActivator()
{
// Arrange
var serviceProvider = GetServiceProvider();
var pageActivator = Assert.IsType<RazorPageActivator>(serviceProvider.GetRequiredService<IRazorPageActivator>());
var hotReload = serviceProvider.GetRequiredService<RazorHotReload>();
var cache = pageActivator.ActivationInfo;
cache[new RazorPageActivator.CacheKey()] = new RazorPagePropertyActivator(
typeof(string), typeof(object),
new EmptyModelMetadataProvider(),
new RazorPagePropertyActivator.PropertyValueAccessors());
// Act
Assert.Single(pageActivator.ActivationInfo);
hotReload.ClearCache(Type.EmptyTypes);
// Assert
Assert.Empty(pageActivator.ActivationInfo);
}
private static ServiceProvider GetServiceProvider()
{
var diagnosticListener = new DiagnosticListener("Microsoft.AspNetCore");
var serviceProvider = new ServiceCollection()
.AddControllersWithViews()
.Services
// Manually add RazorHotReload because it's only added if MetadataUpdateHandler.IsSupported = true
.AddSingleton<RazorHotReload>()
.AddSingleton<ILoggerFactory>(NullLoggerFactory.Instance)
.AddSingleton<DiagnosticSource>(diagnosticListener)
.AddSingleton(diagnosticListener)
.BuildServiceProvider();
return serviceProvider;
}
}