Skip to content

[Blazor] Clear caches on HotReload #62880

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/Components/Authorization/src/AttributeAuthorizeDataCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,24 @@

using System.Collections.Concurrent;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Components.HotReload;

namespace Microsoft.AspNetCore.Components.Authorization;

internal static class AttributeAuthorizeDataCache
{
static AttributeAuthorizeDataCache()
{
if (HotReloadManager.Default.MetadataUpdateSupported)
{
HotReloadManager.Default.OnDeltaApplied += ClearCache;
}
}

private static readonly ConcurrentDictionary<Type, IAuthorizeData[]?> _cache = new();

private static void ClearCache() => _cache.Clear();

public static IAuthorizeData[]? GetAuthorizeDataForType(Type type)
{
if (!_cache.TryGetValue(type, out var result))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@
<Reference Include="Microsoft.AspNetCore.Components" />
</ItemGroup>

<ItemGroup>
<Compile Include="$(ComponentsSharedSourceRoot)src\HotReloadManager.cs" LinkBase="HotReload" />
</ItemGroup>

</Project>
17 changes: 17 additions & 0 deletions src/Components/Components/src/BindConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Reflection;
using System.Text;
using System.Text.Json;
using Microsoft.AspNetCore.Components.HotReload;

namespace Microsoft.AspNetCore.Components;

Expand Down Expand Up @@ -1667,6 +1668,14 @@ private static class FormatterDelegateCache
{
private static readonly ConcurrentDictionary<Type, Delegate> _cache = new ConcurrentDictionary<Type, Delegate>();

static FormatterDelegateCache()
{
if (HotReloadManager.Default.MetadataUpdateSupported)
{
HotReloadManager.Default.OnDeltaApplied += _cache.Clear;
}
}

private static MethodInfo? _makeArrayFormatter;

[UnconditionalSuppressMessage(
Expand Down Expand Up @@ -1856,6 +1865,14 @@ internal static class ParserDelegateCache
{
private static readonly ConcurrentDictionary<Type, Delegate> _cache = new ConcurrentDictionary<Type, Delegate>();

static ParserDelegateCache()
{
if (HotReloadManager.Default.MetadataUpdateSupported)
{
HotReloadManager.Default.OnDeltaApplied += _cache.Clear;
}
}

private static MethodInfo? _convertToEnum;
private static MethodInfo? _convertToNullableEnum;
private static MethodInfo? _makeArrayTypeConverter;
Expand Down
9 changes: 9 additions & 0 deletions src/Components/Components/src/ComponentFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Concurrent;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using Microsoft.AspNetCore.Components.HotReload;
using Microsoft.AspNetCore.Components.Reflection;
using Microsoft.AspNetCore.Components.RenderTree;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -18,6 +19,14 @@ private const BindingFlags _injectablePropertyBindingFlags

private static readonly ConcurrentDictionary<Type, ComponentTypeInfoCacheEntry> _cachedComponentTypeInfo = new();

static ComponentFactory()
{
if (HotReloadManager.Default.MetadataUpdateSupported)
{
HotReloadManager.Default.OnDeltaApplied += ClearCache;
}
}

private readonly IComponentActivator _componentActivator;
private readonly Renderer _renderer;

Expand Down
9 changes: 9 additions & 0 deletions src/Components/Components/src/DefaultComponentActivator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Collections.Concurrent;
using System.Diagnostics.CodeAnalysis;
using Microsoft.AspNetCore.Components.HotReload;
using Microsoft.Extensions.DependencyInjection;

namespace Microsoft.AspNetCore.Components;
Expand All @@ -11,6 +12,14 @@ internal sealed class DefaultComponentActivator(IServiceProvider serviceProvider
{
private static readonly ConcurrentDictionary<Type, ObjectFactory> _cachedComponentTypeInfo = new();

static DefaultComponentActivator()
{
if (HotReloadManager.Default.MetadataUpdateSupported)
{
HotReloadManager.Default.OnDeltaApplied += ClearCache;
}
}

public static void ClearCache() => _cachedComponentTypeInfo.Clear();

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using Microsoft.AspNetCore.Components.HotReload;
using Microsoft.AspNetCore.Components.Reflection;
using Microsoft.AspNetCore.Internal;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -24,6 +25,14 @@ internal sealed class PersistentServicesRegistry
private List<PersistingComponentStateSubscription> _subscriptions = [];
private static readonly ConcurrentDictionary<Type, PropertiesAccessor> _cachedAccessorsByType = new();

static PersistentServicesRegistry()
{
if (HotReloadManager.Default.MetadataUpdateSupported)
{
HotReloadManager.Default.OnDeltaApplied += _cachedAccessorsByType.Clear;
}
}

public PersistentServicesRegistry(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
Expand Down
15 changes: 15 additions & 0 deletions src/Components/Components/src/PersistentStateValueProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using Microsoft.AspNetCore.Components.HotReload;
using Microsoft.AspNetCore.Components.Reflection;
using Microsoft.AspNetCore.Components.Rendering;
using Microsoft.AspNetCore.Internal;
Expand All @@ -21,6 +22,20 @@ internal sealed class PersistentStateValueProvider(PersistentComponentState stat
private static readonly ConcurrentDictionary<(Type, string), PropertyGetter> _propertyGetterCache = new();
private static readonly ConcurrentDictionary<Type, IPersistentComponentStateSerializer?> _serializerCache = new();

static PersistentStateValueProvider()
{
if (HotReloadManager.Default.MetadataUpdateSupported)
{
HotReloadManager.Default.OnDeltaApplied += ClearCaches;
}
}

private static void ClearCaches()
{
_propertyGetterCache.Clear();
_serializerCache.Clear();
}

private readonly Dictionary<ComponentState, PersistingComponentStateSubscription> _subscriptions = [];

public bool IsFixed => false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,21 @@
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using Microsoft.AspNetCore.Components.HotReload;
using static Microsoft.AspNetCore.Internal.LinkerFlags;

namespace Microsoft.AspNetCore.Components.Reflection;

internal static class ComponentProperties
{
static ComponentProperties()
{
if (HotReloadManager.Default.MetadataUpdateSupported)
{
HotReloadManager.Default.OnDeltaApplied += ClearCache;
}
}

internal const BindingFlags BindablePropertyFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.IgnoreCase;

// Right now it's not possible for a component to define a Parameter and a Cascading Parameter with
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@

using System.Collections.Concurrent;
using System.Reflection;
using Microsoft.AspNetCore.Components.HotReload;

namespace Microsoft.AspNetCore.Components.RenderTree;

internal static class EventArgsTypeCache
{
private static readonly ConcurrentDictionary<MethodInfo, Type> Cache = new ConcurrentDictionary<MethodInfo, Type>();

static EventArgsTypeCache()
{
if (HotReloadManager.Default.MetadataUpdateSupported)
{
HotReloadManager.Default.OnDeltaApplied += Cache.Clear;
}
}

public static Type GetEventArgsType(MethodInfo methodInfo)
{
return Cache.GetOrAdd(methodInfo, methodInfo =>
Expand Down
9 changes: 9 additions & 0 deletions src/Components/Components/src/Routing/RouteTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Concurrent;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using Microsoft.AspNetCore.Components.HotReload;
using Microsoft.AspNetCore.Routing.Tree;

namespace Microsoft.AspNetCore.Components.Routing;
Expand All @@ -13,6 +14,14 @@ internal sealed class RouteTable(TreeRouter treeRouter)
private readonly TreeRouter _router = treeRouter;
private static readonly ConcurrentDictionary<(Type, string), InboundRouteEntry> _routeEntryCache = new();

static RouteTable()
{
if (HotReloadManager.Default.MetadataUpdateSupported)
{
HotReloadManager.Default.OnDeltaApplied += _routeEntryCache.Clear;
}
}

public TreeRouter? TreeRouter => _router;

[UnconditionalSuppressMessage(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<Compile Include="$(ComponentsSharedSourceRoot)src\DefaultAntiforgeryStateProvider.cs" LinkBase="Forms" />
<Compile Include="$(SharedSourceRoot)LinkerFlags.cs" LinkBase="Shared" />
<Compile Include="$(SharedSourceRoot)Components\ComponentsActivityLinkStore.cs" LinkBase="Shared" />
<Compile Include="$(ComponentsSharedSourceRoot)src\HotReloadManager.cs" LinkBase="HotReload" />

<Compile Include="$(SharedSourceRoot)PropertyHelper\**\*.cs" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Reflection;
using System.Reflection.Metadata;
using Microsoft.AspNetCore.Components.Endpoints;
using Microsoft.AspNetCore.Components.HotReload;
using Microsoft.AspNetCore.Components.Rendering;
using Microsoft.AspNetCore.Components.RenderTree;

Expand All @@ -15,6 +16,15 @@ namespace Microsoft.AspNetCore.Components.Endpoints;
internal sealed class EndpointComponentState : ComponentState
{
private static readonly ConcurrentDictionary<Type, StreamRenderingAttribute?> _streamRenderingAttributeByComponentType = new();

static EndpointComponentState()
{
if (HotReloadManager.Default.MetadataUpdateSupported)
{
HotReloadManager.Default.OnDeltaApplied += _streamRenderingAttributeByComponentType.Clear;
}
}

private readonly EndpointHtmlRenderer _renderer;
public EndpointComponentState(Renderer renderer, int componentId, IComponent component, ComponentState? parentComponentState)
: base(renderer, componentId, component, parentComponentState)
Expand Down
5 changes: 4 additions & 1 deletion src/Components/Forms/src/FieldIdentifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ namespace Microsoft.AspNetCore.Components.Forms;

static FieldIdentifier()
{
HotReloadManager.Default.OnDeltaApplied += ClearCache;
if (HotReloadManager.Default.MetadataUpdateSupported)
{
HotReloadManager.Default.OnDeltaApplied += ClearCache;
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ internal static class ExpressionFormatter
{
static ExpressionFormatter()
{
HotReloadManager.Default.OnDeltaApplied += ClearCache;
if (HotReloadManager.Default.MetadataUpdateSupported)
{
HotReloadManager.Default.OnDeltaApplied += ClearCache;
}
}

internal const int StackAllocBufferSize = 128;
Expand Down
Loading